题目链接
题解
额...
考你会不会离散化优化常数
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9')c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
const int maxn = 2000007;
int n;
int x[maxn],y[maxn],z[maxn];
int tmp[maxn];
int fa[maxn];
int find(int x ) {
if(fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
void solve() {
n = read();
int tot = 0;
for(int i = 1;i <= n;++ i) {
x[i] = read(),y[i] = read();z[i] = read();
tmp[++ tot] = x[i],tmp[++ tot] = y[i];
}
std::sort(tmp + 1,tmp + tot + 1);
tot = std::unique(tmp + 1,tmp + tot + 1) - tmp - 1;
for(int i = 1;i <= tot;++ i) fa[i] = i;
for(int i = 1;i <= n;++ i) {
x[i] = std::lower_bound(tmp + 1,tmp + tot + 1,x[i]) - tmp,
y[i] = std::lower_bound(tmp + 1,tmp + tot + 1,y[i]) - tmp;
if(z[i]) fa[find(x[i])] = find(y[i]);
}
//for(int i = 1;i <= n;++ i) if(z[i]) fa[find(x[i])] = find(y[i]);
for(int i = 1;i <= n;++ i)
if(!z[i] && find(x[i]) == find(y[i])) {
puts("NO");
return;
}
puts("YES");
}
int main() {
int t = read();
for(int i = 1;i <= t;++ i)
solve();
}