程序自动分析(并查集 + 离散化)
感觉正常写即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 10; int t,n,a,b,e,fa[maxn],aa[maxn],cnt,ssize; vector<pair<int,int> >va,vb; int find(int x){ return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int a,int b){ fa[find(a)] = find(b); } int main(){ //freopen("in","r",stdin); ios::sync_with_stdio(0); cin >> t; while(t--) { va.clear();vb.clear(); cnt = 0; int flag = 1; cin >> n; for(int i = 1; i <= n; i++){ cin >> a >> b >> e; if(e) va.push_back(make_pair(a,b)); else { aa[++cnt] = a,aa[++cnt] = b; vb.push_back(make_pair(a,b)); } } sort(aa + 1,aa + 1 + cnt); ssize = unique(aa + 1,aa + 1 + cnt) - aa - 1; for(int i = 1; i <= ssize; i++) fa[i] = i; for(int i = 0; i < va.size(); i++){ a = lower_bound(aa + 1, aa + 1 + ssize,va[i].first) - aa; b = lower_bound(aa + 1, aa + 1 + ssize,va[i].second) - aa; merge(a,b); } for(int i = 0; i < vb.size(); i++){ a = lower_bound(aa + 1, aa + 1 + ssize,vb[i].first) - aa; b = lower_bound(aa + 1, aa + 1 + ssize,vb[i].second) - aa; if(find(a) == find(b)){ flag = 0; break; } } cout << (flag == 1 ?"YES" : "NO") << endl; } return 0; }