离散化+并查集
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
const int N = 2e5;
int t;
int n;
vector<int> all;
vector<PII> ye;
vector<PII> no;
int f[N];
int find(int x)
{
if(f[x]!=x) f[x]=find(f[x]);
return f[x];
}
int findid(int x)
{
return lower_bound(all.begin(),all.end(),x)-all.begin();
}
void check()
{
for(auto it:ye)
{
int x=findid(it.x),y=findid(it.y);
int fax=find(x),fay=find(y);
if(fax!=fay) f[fax]=fay;
}
bool cannot=false;
for(auto it:no){
int x=findid(it.x),y=findid(it.y);
int fax=find(x),fay=find(y);
if(fax==fay){
cannot=true;break;
}
}
if(cannot) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n;
all.clear();
ye.clear();
no.clear();
for(int i=0;i<N;i++) f[i]=i;
while (n -- )
{
int x,y,op;cin>>x>>y>>op;
all.push_back(x);all.push_back(y);
if(op==1) ye.push_back({x,y});
else no.push_back({x,y});
}
sort(all.begin(),all.end());
all.erase(unique(all.begin(),all.end()),all.end());
check();
}
return 0;
}