#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 +5;
int T, n, a[N], xx[N] , yy[N] , zz[N];
map<int, int> fa;
int find(int x){
return fa[x] = (fa[x] == x)? (x) : (find(fa[x]));
}
void merge(int a, int b){
fa[find(a)] = find(b);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> T;
while(T --){
fa.clear();
cin >> n;
for(int i = 1;i <= n;i ++){
int xx[i], yy[i], zz[i];
cin >>xx[i] >> yy[i] >>zz[i];
fa[xx[i]] = xx[i], fa[yy[i]] = yy[i];
}
for(int i = 1; i <= n ; i++){
if(zz[i]){
merge(xx[i], yy[i]);
}
}
bool flag = 0;
for(int i = 1;i <= n;i ++){
if(!zz[i]){
int fx = find(xx[i]), fy = find(yy[i]);
if(fx == fy){
flag = 1;
break;
}
}
}
if(flag)cout << "NO" << endl;
else cout << "YES" << endl;
}
return 0;
}
并查集做的,大佬们求助