#P1955. [NOI2015] 程序自动分析

    ID: 901 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数据结构并查集其他离散化2015NOI 系列哈希HASH

[NOI2015] 程序自动分析

Description

During the implementation of automatic program analysis, it is often necessary to determine whether certain constraints can be satisfied simultaneously.

Consider a simplified version of a constraint satisfaction problem: suppose x1,x2,x3,x_1, x_2, x_3, \cdots represent variables that appear in the program. Given nn constraints of the form xi=xjx_i = x_j or xixjx_i \neq x_j, determine whether it is possible to assign an appropriate value to each variable so that all the constraints are satisfied simultaneously. For example, if the constraints in one problem are x1=x2,x2=x3,x3=x4,x4x1x_1 = x_2, x_2 = x_3, x_3 = x_4, x_4 \neq x_1, these constraints are clearly impossible to satisfy at the same time, so this problem should be judged as unsatisfiable.

Now you are given several constraint satisfaction problems. Please determine each of them.

Input Format

The first line contains a positive integer tt, indicating the number of problems to judge. Note that these problems are independent of each other.

For each problem, there are several lines:

The first line contains a positive integer nn, indicating the number of constraints that need to be satisfied in this problem. The next nn lines each contain three integers i,j,ei, j, e, describing one equality/inequality constraint, with a single space between adjacent integers. If e=1e = 1, then the constraint is xi=xjx_i = x_j. If e=0e = 0, then the constraint is xixjx_i \neq x_j.

Output Format

Output tt lines.

On the kk-th line, output the string YES or NO (all uppercase). YES means the kk-th problem in the input is judged satisfiable, and NO means it is unsatisfiable.

2
2
1 2 1
1 2 0
2
1 2 1
2 1 1
NO
YES

2
3
1 2 1
2 3 1
3 1 1
4
1 2 1
2 3 1
3 4 1
1 4 0

YES
NO

Hint

[Sample Explanation 1]

In the first problem, the constraints are x1=x2,x1x2x_1 = x_2, x_1 \neq x_2. These two constraints contradict each other, so they cannot be satisfied simultaneously.

In the second problem, the constraints are x1=x2,x1=x2x_1 = x_2, x_1 = x_2. These two constraints are equivalent and can be satisfied simultaneously.

[Sample Explanation 2]

In the first problem, there are three constraints: x1=x2,x2=x3,x3=x1x_1 = x_2, x_2 = x_3, x_3 = x_1. It suffices to assign values so that x1=x2=x3x_1 = x_2 = x_3, and all constraints are satisfied.

In the second problem, there are four constraints: x1=x2,x2=x3,x3=x4,x4x1x_1 = x_2, x_2 = x_3, x_3 = x_4, x_4 \neq x_1. From the first three constraints we can derive x1=x2=x3=x4x_1 = x_2 = x_3 = x_4, but the last constraint requires x1x4x_1 \neq x_4, so it is unsatisfiable.

[Constraints]

All testdata ranges and characteristics are as follows:

::cute-table{tuack} | Test point id | Range of nn | Range of i,ji, j | Conventions | | :-: | :-: | :-: | :-: | | 11 | 1n101 \le n \le 10 | 1i,j1041 \le i, j \le 10^4 | 1t101 \le t \le 10; e{0,1}e \in \{0, 1\} | | 22 | ^ | ^ | ^ | | 33 | 1n1001 \le n \le 100 | ^ | ^ | | 44 | ^ | ^ | ^ | | 55 | 1n1051 \le n \le 10^5 | ^ | ^ | | 66 | ^ | ^ | ^ | | 77 | ^ | ^ | ^ | | 88 | 1n1051 \le n \le 10^5 | 1i,j1091 \le i, j \le 10^9 | ^ | | 99 | ^ | ^ | ^ | | 1010 | ^ | ^ | ^ |

Translated by ChatGPT 5