HDU - 5971 Wrestling Match (种类并查集)

Wrestling Match

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3367    Accepted Submission(s): 1210


Problem Description

Nowadays, at least one wrestling match is held every year in our country. There are a lot of people in the game is "good player”, the rest is "bad player”. Now, Xiao Ming is referee of the wrestling match and he has a list of the matches in his hand. At the same time, he knows some people are good players,some are bad players. He believes that every game is a battle between the good and the bad player. Now he wants to know whether all the people can be divided into "good player" and "bad player".

Input

Input contains multiple sets of data.For each set of data,there are four numbers in the first line:N (1 ≤ N≤ 1000)、M(1 ≤M ≤ 10000)、X,Y(X+Y≤N ),in order to show the number of players(numbered 1toN ),the number of matches,the number of known "good players" and the number of known "bad players".In the next M lines,Each line has two numbersa, b(a≠b) ,said there is a game between a and b .The next line has X different numbers.Each number is known as a "good player" number.The last line contains Y different numbers.Each number represents a known "bad player" number.Data guarantees there will not be a player number is a good player and also a bad player.

Output

If all the people can be divided into "good players" and "bad players”, output "YES", otherwise output "NO".

Sample Input

5 4 0 0 1 3 1 4 3 5 4 5 5 4 1 0 1 3 1 4 3 5 4 5 2

Sample Output

NO YES

Source

2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

先吐槽一下这个题的数据

8 3 1 1

1 2

3 4

5 6

7 8

这组数据应该输出的是NO吧,虽然前6个点能形成二分图,但是形成了3个二分图啊,我们并不能把他们分成两组啊

网上的其它题解几乎全输出的YES,我有点怀疑我题意理解错了,如果我理解错了,请给我指出

我用的种类并查集写的,两个节点的关系为1代表属于同一组,两个节点关系为0代表不属于同一组,m场比赛每场比赛的两个节点都不属于同一组,join的同时判断是否和之前的关系矛盾,最后给出a个好人,b个坏人,每个好人与b个坏人join一下,a个好人互相join一下,b个坏人互相join一下,同时判断矛盾,标记以上所有出现的点,最后判断如果存在没有被标记的,就直接输出NO,最后还要判断一下这个种类并查集中有多少个连通块,特殊情况如果某一个连通块存在一个已经明确好人坏人的点,就不计数,连通块大于1输出NO

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int pre[MAXN],re[MAXN];
int G[MAXN],B[MAXN];
int vis[MAXN];
int vis1[MAXN];
int Find(int x)
{
	if(x == pre[x]) {
		return x;
	}
	else {
		int t = pre[x];
		pre[x] = Find( pre[x] );
		int rf = re[x];
		int rff = re[t];
		re[x] = (rf == rff);
		return pre[x];
	}
}
bool join(int x,int y,int r)
{
	int fx = Find(x);
	int fy = Find(y);
	if(fx != fy) {
		pre[fy] = fx;
		re[fy]  =  ( re[y] + re[x] ) % 2;
	}
	else {
		if( re[x] == re[y] ) {
			return false;
		}
	}
	return true;
}
int main(void)
{
	int n,m,a,b;
	int u,v;
	while(scanf("%d %d %d %d",&n,&m,&a,&b) != EOF) {
		for(int i = 1; i <= n; i++) {
			pre[i] = i;
			re[i] = 1;
			vis[i] = 0;
			vis1[i] = 0;
		}
		int flag = 0;
		while(m--) {
			scanf("%d %d",&u,&v);
			vis[u] = 1;
			vis[v] = 1;
			if( !flag ) if( !join(u,v,0) ) flag = 1;
		}
		for(int i = 1; i <= a; i++) {
			scanf("%d",&G[i]);
			vis[G[i]] = 1;
		}
		for(int i = 1; i <= b; i++) {
			scanf("%d",&B[i]);
			vis[B[i]] = 1;
		}
		if(!flag) {
			if(a) {
				int uu = G[1];
				for(int i = 2; i <= a; i++) {
					if( !flag ) if( !join(uu,G[i],1)) flag = 1;
				}
			}
			if(b) {
				int uu = G[1];
				for(int i = 2; i <= b; i++) {
					if( !flag ) if( !join(uu,B[i],1)) flag = 1;
				}
			}
			for(int i = 1; i <= a; i++) {
				for(int j = 1; j <= b; j++) {
					if( !flag ) if( !join(G[i],G[j],0) ) flag = 1;
				}
			}

		}
		for(int i = 1;i <= n; i++) {
			if(!vis[i]) {
				flag = 1;
			}
		}
		for(int i = 1; i <= a; i++) {
			vis1[Find(G[i])] = 1;
		}
		for(int i = 1; i <= b; i++) {
			vis1[Find(B[i])] = 1;
		}
		int cnt = 0;
		for(int i = 1; i <= n; i++) {
			int t = Find(i);
			if(i == t && !vis1[t]) {
				cnt++;
			}
		}
		if(cnt > 1) flag = 1;
		if(flag) printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值