#d2952. 程序自动分析

程序自动分析

Program Auto-analysis

Problem Description

When implementing program auto-analysis, we often need to decide whether a set of constraints can be satisfied simultaneously.

Consider a simplified constraint satisfaction problem: let x1,x2,x3,…x_1,x_2,x_3,\dots be the variables appearing in a program. You are given nn constraints of the form xi=xjx_i = x_j or xi≠xjx_i \neq x_j (variable equality / inequality) and must determine whether it is possible to assign a suitable value to every variable so that all the constraints are satisfied at the same time.

For example, if the constraints are x1=x2, x2=x3, x3=x4, x1≠x4x_1 = x_2,\ x_2 = x_3,\ x_3 = x_4,\ x_1 \neq x_4, it is clearly impossible to satisfy them all, so the answer should be "not satisfiable".

You are given several such constraint satisfaction problems; determine each of them.

Input Format

The first line of the input contains a positive integer tt (1≤t≤105)(1 \leq t \leq 10^5), the number of problems to judge. Note that these problems are independent of one another.

For each problem, the following lines are given:

  • The first line contains a positive integer nn (1≤n≤105)(1 \leq n \leq 10^5), the number of constraints in this problem.
  • The next nn lines each contain three integers i,j,ei, j, e (1≤i,j≤109, 0≤e≤1)(1 \leq i, j \leq 10^9,\ 0 \leq e \leq 1), describing one equality / inequality constraint, with adjacent integers separated by a single space. If e=1e = 1 the constraint is xi=xjx_i = x_j; if e=0e = 0 the constraint is xi≠xjx_i \neq x_j.

∑n≤2.1×105\sum n \leq 2.1 \times 10^5

Output Format

The output file contains tt lines.

The kk-th line of the output contains the string YES or NO. YES means the kk-th problem in the input is judged to be satisfiable, and NO means it is not.

Sample Input

2
2
1 2 1
1 2 0
2
1 2 1
2 1 1

Sample Output

NO
YES

Constraints

  • 1≤t≤1051 \leq t \leq 10^5
  • 1≤n≤1051 \leq n \leq 10^5 for each problem, ∑n≤2.1×105\sum n \leq 2.1 \times 10^5
  • 1≤i,j≤1091 \leq i, j \leq 10^9, 0≤e≤10 \leq e \leq 1
  • The problems are independent of one another