#K68017. Divide the Social Network
Divide the Social Network
Divide the Social Network
In this problem, you are given a social network consisting of N residents (numbered from 0 to N-1) and M relationships among them. Each relationship is described by three integers A, B, and R. If R = 1, residents A and B are friends, meaning they should be placed in the same group. If R = -1, they are enemies and must be placed in different groups. Your task is to determine whether it is possible to divide all residents into two groups such that all friends are in the same group and all enemies are in opposite groups.
More formally, you must assign each resident a color (0 or 1) such that for every relationship: [ \text{if } R = 1, \quad color[A] = color[B], \quad \text{and if } R = -1, \quad color[A] \neq color[B]. ]
Print YES
if such a partition is possible, or NO
otherwise.
inputFormat
The first line contains two integers N and M, representing the number of residents and the number of relationships respectively. Each of the following M lines contains three integers A, B, and R, describing a relationship. Residents are numbered from 0 to N-1. It is guaranteed that R is either 1 or -1.
outputFormat
Output a single line containing YES
if it is possible to partition the residents as described, or NO
otherwise.## sample
4 4
0 1 1
1 2 -1
2 3 1
3 0 -1
YES
</p>