시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB130272422.018%

문제

Let $x_1, x_2, x_3, \dots$ be variables. $n$ constraints of form $x_i = x_j$ or $x_i \ne x_j$ are given. The task asks for whether it is possible to assign values to the variables so that all constraints can be satisfied. For example, if the constraints are $x_1 = x_2, x_2 = x_3, x_3 = x_4, x_1 \ne x_4$, then those constraints cannot be satisfied simultaneously.

입력

The first line of the input is an integer $t$ representing the number of instances to solve. The instances are independent. For each instance, the first line is an integer $n$ representing the number of constraints to be satisfied. In the following $n$ lines, each line has three integers $i,j,e$ representing an equality/inequality constraint. If $e = 1$, the constraint shall be $x_i = x_j$. If $e = 0$, the constraint shall be $x_i \ne x_j$.

출력

The output has $t$ lines. The $k$-th line of the output is a string YES or NO. Output YES if the constraints in that instance can be satisfied and NO otherwise.

제한

  • $1 \le n \le 100\,000$
  • $1 \le i, j \le 1\,000\,000\,000$
  • $1 \le t \le 10$
  • $e \in \{0,1\}$

예제 입력 1

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

예제 출력 1

NO
YES

예제 입력 2

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

예제 출력 2

YES
NO