//要先处理=再判断!=
//离散化后并的是地址而非数值
//low_bound可查询特定数值所在的地址,和map差不多?
//思考查找其他离散化的方法
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, t, tot;
int f[2*maxn], e[2*maxn];
struct node{
int x, y, flg;
} a[maxn];
inline void getInit(){
int nn = 2*n;
for(int i = 0; i <= nn; i++) f[i] = i;
tot = 0;
}
inline int find(int x){
if(x == f[x]) return x;
return f[x] = find(f[x]);
}
inline void merge(int x, int y){
f[find(x)] = f[find(y)];
}
inline bool ask(int x, int y){
return find(x) == find(y);
}
int main(){
scanf("%d", &t);
while(t--){
bool flag = 0;
scanf("%d", &n);
getInit();
for(int i = 1; i <= n; i++){
scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].flg);
e[++tot] = a[i].x;
e[++tot] = a[i].y;
//if(a[i].flg) merge(a[i].x, a[i].y);
}
sort(e+1, e+tot+1);
for(int i = 1; i <= n; i++)
if(a[i].flg)
merge(lower_bound(e+1, e+tot+1, a[i].x)-e, lower_bound(e+1, e+tot+1, a[i].y)-e);
for(int i = 1; i <= n; i++)
if(!a[i].flg)
if(ask(lower_bound(e+1, e+tot+1, a[i].x)-e, lower_bound(e+1, e+tot+1, a[i].y)-e)){
printf("NO\n");
flag = 1;
break;
}
if(!flag) printf("YES\n");
}
return 0;
}
NOI 2015 程序自动分析
最新推荐文章于 2023-01-04 16:12:29 发布