并查集小水题~
需要Hash表/map/离散化一下
一开始没排序…sb了。。。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200011
inline char gc(){
static char buf[1<<16],*S,*T;
if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int n,owo=0,h[N],num=0,fa[N],n1,n2;
struct Hash_table{
int key,val,next;
}data[N];
struct quer{
int x,y;
quer(){}
quer(int _x,int _y){x=_x;y=_y;}
}a[N>>1],b[N>>1];
inline int hs(int key){
int x=key%N;
for(int i=h[x];i;i=data[i].next)
if(data[i].key==key) return data[i].val;
data[++num].key=key;data[num].val=++owo;data[num].next=h[x];h[x]=num;
fa[owo]=owo;return owo;
}
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
int main(){
// freopen("a.in","r",stdin);
int tst=read();
while(tst--){
n=read();memset(h,0,sizeof(h));num=0;owo=0;bool flag=1;n1=0,n2=0;
for(int i=1;i<=n;++i){
int x=hs(read()),y=hs(read()),op=read();
if(op) a[++n1]=quer(x,y);
else b[++n2]=quer(x,y);
}for(int i=1;i<=n1;++i){
int xx=find(a[i].x),yy=find(a[i].y);
if(xx!=yy) fa[xx]=yy;
}for(int i=1;i<=n2;++i){
if(find(b[i].x)==find(b[i].y)) flag=0;
}puts(flag?"YES":"NO");
}return 0;
}