【带权并查集】Gym - 100923H - Por Costel and the Match

裸题。

看之前的模版讲解吧,这里不再赘述了。

#include<cstdio>
#include<cstring>
using namespace std;
int fa[100010];
bool rel[100010];
int n,m,T;
int findroot(int x){
	if(x==fa[x]){
		return x;
	}
	int Fa=findroot(fa[x]);
	if(rel[x]==0 && rel[fa[x]]==0){
		rel[x]=0;
	}
	else if(rel[x]==0 && rel[fa[x]]==1){
		rel[x]=1;
	}
	else if(rel[x]==1 && rel[fa[x]]==0){
		rel[x]=1;
	}
	else if(rel[x]==1 && rel[fa[x]]==1){
		rel[x]=0;
	}
	return fa[x]=Fa;
}
int main(){
//	freopen("meciul.in","r",stdin);
//	freopen("meciul.out","w",stdout);
	int x,y;
	scanf("%d",&T);
	for(;T;--T){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;++i){
			fa[i]=i;
		}
		memset(rel,0,sizeof(rel));
		for(int i=1;i<=m;++i){
			scanf("%d%d",&x,&y);
			int f1=findroot(x),f2=findroot(y);
			if(f1==f2){
				if(rel[x]==rel[y]){
					puts("NO");
				}
				else{
					puts("YES");
				}
			}
			else{
				fa[f1]=f2;
				if(rel[x]==0 && rel[y]==0){
					rel[f1]=1;
				}
				else if(rel[x]==0 && rel[y]==1){
					rel[f1]=0;
				}
				else if(rel[x]==1 && rel[y]==0){
					rel[f1]=0;
				}
				else if(rel[x]==1 && rel[y]==1){
					rel[f1]=1;
				}
				puts("YES");
			}
		}
	}
	return 0;
}

转载于:https://www.cnblogs.com/autsky-jadek/p/6551571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值