【玄关】90pts求调
查看原帖
【玄关】90pts求调
659165
mysterys楼主2024/8/7 22:16

RTRT,有事@我。

lz 要去睡觉了,可能明天早上才会回。

#include<bits/stdc++.h>
using namespace std;
#define FILE(x) freopen(x".in", "r", stdin);freopen(x".out", "w", stdout);
#define endl '\n'
typedef long long ll;
const int N=2e5+10;
int fa[N<<1];
unordered_map<int,int> mp;
int n=N,m,t;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void init(){
	for(int i=1;i<=2*n;i++) fa[i]=i;
	t=0;mp.clear();
}
inline void unionn(int x,int y){
	x=find(x);y=find(y);
	if(x!=y){
		fa[x]=y;
	}
}
inline void solve(){
	init();
	cin>>m;
	while(m--){
		int i,j,opt;
		cin>>i>>j>>opt;
		if(!mp[i]){
			mp[i]=++t;
		}
		if(!mp[j]){
			mp[j]=++t;
		}
		if(opt==1){
			unionn(mp[i],mp[j]);
			unionn(mp[i]+n,mp[j]+n);
		}else {
			unionn(mp[i]+n,mp[j]);unionn(mp[i],mp[j]+n);
		}
	}
	for(int i=1;i<=t;i++){
		if(find(i)==find(i+n)){
			cout<<"NO"<<endl;
			return;
		}
	}
	cout<<"YES"<<endl;
}
signed main(){
	//freopen("P1955_3.in","r",stdin);
	cin.tie(nullptr)->ios::sync_with_stdio(false);
	cout.tie(nullptr);
	int T;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}
in:
10
1
1 2 1
1
2 2 0
10
1 2 1
2 3 1
3 5 1
5 10 1
10 100 1
10000 100 1
1 9999 0
3 2 1
10000 1 0
2 3 1
4
1 7 1
9 7 0
13 9 1
1 13 1
5
7 9 0
9 7 0
3 5 0
1 7 0
2 4 0
9
24 234 1
2837 1 1
235 877 1
242 78 0
23 1 1
223 977 0
254 76 1
235 987 0
877 987 1
9
24 234 1
2837 1 1
242 78 0
23 1 1
223 977 0
254 76 1
235 877 0
235 987 0
877 987 0
4
8 2 0
2 9 1
2 7 1
9 7 0
2
1 1 1
2 2 1
3
9 2 0
1 3 1
2 3 1

out:
YES
NO
NO
NO
YES
NO
YES
NO
YES
YES

lz 代码 out:

YES
NO
NO
NO
YES
NO
NO
NO
YES
YES
2024/8/7 22:16
加载中...