在你们看我代码的时候我可能已经做出来了,注意评论
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = (int)1e9+100;
unordered_map<long long, long long> fa;
long long findRt(long long x) {
return fa[x] = (fa[x]==x?x:findRt(fa[x]));
}
void conv(long long x, long long y) {
long long fx, fy;
fx = findRt(x);
fy = findRt(y);
if(fx!=fy) {
if(fx > fy) {
swap(fx, fy);
}
fa[fy] = fx;
}
}
int main() {
int t;
cin >> t;
while (t--) {
fa.clear();
int n;
scanf("%d", &n);
long long t, x, y;
bool s = true;
for(int i=0; i<n; i++) {
scanf("%lld%lld%lld", &x, &y, &t);
if(!s) {
continue;
}
if(fa[x]==0) {
fa[x] = x;
fa[x+N] = x+N;
}
if(fa[y]==0) {
fa[y] = y;
fa[y+N] = y+N;
}
x = findRt(x);
y = findRt(y);
if(t==1) {
if(findRt(x) == findRt(y+N)) {
s = false;
continue;
}
conv(x, y);
conv(x+N, y+N);
}
if(t==0) {
if(findRt(x) == findRt(y)) {
s = false;
continue;
}
conv(x, y+N);
conv(x+N, y);
}
}
if(s) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}