#include<bits/stdc++.h>
#define ll long long
const int N=1000007;
const int M=1145141;
using namespace std;
int lsh[M<<1];
int lsh2[M<<1];
int f[M<<1];
int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void unionn(int x,int y)
{
int fx=find(x),fy=find(y);
f[fx]=fy;
}
int t;
int n;
struct node{
int a,b,e;
}KOBE[1145141<<1];
bool cmp(node a,node b)
{
return a.e>b.e;
}
int main(){
ios::sync_with_stdio(0);
cin>>t;
while(t--)
{
memset(lsh,0,sizeof(lsh));
memset(lsh2,0,sizeof(lsh2));
memset(f,0,sizeof(f));
cin>>n;
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=n;i++)
{
cin>>KOBE[i].a>>KOBE[i].b>>KOBE[i].e;
lsh[i]=KOBE[i].a;lsh2[i]=KOBE[i].b;
}
sort(lsh+1,lsh+1+n);
sort(lsh2+1,lsh2+1+n);
int t=unique(lsh+1,lsh+1+n)-lsh-1;
int t2=unique(lsh2+1,lsh2+1+n)-lsh2-1;
sort(KOBE+1,KOBE+1+n,cmp);
for(int i=1;i<=n;i++)
{
KOBE[i].a=lower_bound(lsh+1,lsh+1+t,KOBE[i].a)-lsh;
KOBE[i].b=lower_bound(lsh2+1,lsh2+1+t2,KOBE[i].b)-lsh2;
}
bool flag=false;
for(int i=1;i<=n;i++)
{
unionn(KOBE[i].a,KOBE[i].b);
if(KOBE[i].e==0){
if((find(KOBE[i].a)==find(KOBE[i].b))){
flag=true;
}
}
}
if(flag==false) cout<<"YES"<<'\n';
else cout<<"NO"<<'\n';
}
return 0;
}
cpp #include<bits/stdc++.h> #define ll long long const int N=1000007; const int M=1145141; using namespace std; int lsh[M<<1]; int lsh2[M<<1]; int f[M<<1]; int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } void unionn(int x,int y) { int fx=find(x),fy=find(y); f[fx]=fy; } int t; int n; struct node{ int a,b,e; }KOBE[1145141<<1]; bool cmp(node a,node b) { return a.e>b.e; } int main(){ ios::sync_with_stdio(0); cin>>t; while(t--) { memset(lsh,0,sizeof(lsh)); memset(lsh2,0,sizeof(lsh2)); memset(f,0,sizeof(f));
cin>>n;
for(int i=1;i<=n;i++) f[i]=i;
for(int i=1;i<=n;i++)
{
cin>>KOBE[i].a>>KOBE[i].b>>KOBE[i].e;
lsh[i]=KOBE[i].a;lsh2[i]=KOBE[i].b;
}
sort(lsh+1,lsh+1+n);
sort(lsh2+1,lsh2+1+n);
int t=unique(lsh+1,lsh+1+n)-lsh-1;
int t2=unique(lsh2+1,lsh2+1+n)-lsh2-1;
sort(KOBE+1,KOBE+1+n,cmp);
for(int i=1;i<=n;i++)
{
KOBE[i].a=lower_bound(lsh+1,lsh+1+t,KOBE[i].a)-lsh;
KOBE[i].b=lower_bound(lsh2+1,lsh2+1+t2,KOBE[i].b)-lsh2;
}
bool flag=false;
for(int i=1;i<=n;i++)
{
unionn(KOBE[i].a,KOBE[i].b);
if(KOBE[i].e==0){
if((find(KOBE[i].a)==find(KOBE[i].b))){
flag=true;
}
}
}
if(flag==false) cout<<"YES"<<'\n';
else cout<<"NO"<<'\n';
}
return 0;
} ^