AcWing 237. 程序自动分析
原题链接
中等
作者:
Value
,
2020-10-14 18:05:41
,
所有人可见
,
阅读 355
离散化 + 并查集
#include <iostream>
#include <cstdio>
#include <unordered_map>
using namespace std;
const int N = 2E6 + 10;
int n, id;
unordered_map<int, int> mp;
struct node{
int x, y, e;
}q[N];
int parent[N];
int get(int x){
if(mp.count(x) == 0) mp[x] = ++ id;
return mp[x];
}
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
int main(){
int T; cin >> T;
while(T -- ){
id = 0;
cin >> n;
mp.clear();
for(int i = 0; i < n; i ++ ){
int x, y, e; scanf("%d%d%d", &x, &y, &e);
q[i] = {get(x), get(y), e};
}
for(int i = 0; i < N; i ++ ) parent[i] = i;
for(int i = 0; i < n; i ++ ){
if(q[i].e == 1) parent[find(q[i].x)] = find(q[i].y);
}
bool res = true;
for(int i = 0; i < n; i ++ ){
if(q[i].e == 0){
int px = find(q[i].x), py = find(q[i].y);
if(px == py){
res = false;
break;
}
}
}
if(res) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}