并查集
只是要离散化或者hash(这个比较慢)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
#define MAXN 2000007
struct point{
int x,y,z;
}a[100100];
int t,n,flag,u,v;
int father[MAXN];
int hashnum[MAXN];
int gethash(int x)
{
int t=x % MAXN;
while (hashnum[t]!=0 && hashnum[t]!=x && t<=MAXN)
t=(t+1) % MAXN;
if (hashnum[t]==x) return t; else {hashnum[t]=x;return t;}
}
int find1(int x)
{
int i=x;
while (father[i]!=i)
i=father[i];
int t;
while (father[x]!=x)
{
t=father[x];
father[x]=i;
x=t;
}
return i;
}
int main()
{
cin>>t;
while (t--)
{
cin>>n;
for (int i=1;i<=MAXN;i++) father[i]=i;
memset(hashnum,0,sizeof(hashnum));
for (int i=1;i<=n;i++)
{
cin>>a[i].x>>a[i].y>>a[i].z;
a[i].x=gethash(a[i].x);
a[i].y=gethash(a[i].y);
if (a[i].z==1)
{
u=find1(a[i].x);
v=find1(a[i].y);
if (u!=v) father[u]=father[v];
}
}
flag=1;
for (int i=1;i<=n;i++)
if (a[i].z==0)
{
u=find1(a[i].x);
v=find1(a[i].y);
if (u==v) {flag=0;break;}
}
if (flag==1) cout<<"YES"; else cout<<"NO";
if (t>0) cout<<endl;
}
return 0;
}