Automated Program Analysis
Problem Description
In the process of implementing program automatic analysis, it is often necessary to determine whether certain constraints can be satisfied simultaneously.
Consider a simplified version of the constraint satisfaction problem:
Assume x1, x2, x3, … represent variables in a program. Given n constraints of the form xi = xj or xi ≠ xj, determine whether it is possible to assign appropriate values to each variable so that all constraints are satisfied simultaneously.
For example, if the constraints are:
x1 = x2, x2 = x3, x3 = x4, x1 ≠ x4,
these constraints obviously cannot be satisfied together, so the problem should be judged as unsatisfiable.
Now, several constraint satisfaction problems are given. Please judge each of them.
Input Format
The first line of input contains a positive integer t, the number of problems to judge. These problems are independent of each other.
For each problem:
- The first line contains a positive integer n, the number of constraints in the problem.
- The next n lines each contain three integers i, j, e, describing one equality/inequality constraint. Adjacent integers are separated by a single space.
- If e = 1, the constraint is xi = xj.
- If e = 0, the constraint is xi ≠ xj.
Output Format
Output t lines.
The k-th line of output should be YES if the k-th problem is satisfiable, otherwise NO.
Data Range
1 ≤ t ≤ 10
1 ≤ n ≤ 105
1 ≤ i, j ≤ 109
Input Sample:
2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
Output Sample:
NO
YES
Comments