【hdu】5971 Wrestling Match - 二分图染色

Wrestling Match

 

题目大意:

有n名球员,m场比赛。其中有x名好球员,y名坏球员。

好球员必定赢过坏球员,问能否把球员明确的区分为好、坏球员。

题解:

思路是二分图染色,当然并查集也可以做。后续有空的话,会补上并查集做法。

利用比赛建图。

先把好、坏球员的分别染色 color[i] = 1 和 color[i] = -1。

①然后依次深搜已染色球员,遇到相同颜色的就输出 “NO”;

②若还有未染色球员,单个点输出 “NO”,否则设为好球员 color[i] = 1进行深搜,重复①。

代码:

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN = 1e4+5;
vector <int > G[MAXN];
int color[MAXN];
int n, m, x, y;
int u, v;

bool dfs(int v, int c){
	color[v] = c;
	for(int i = 0; i < G[v].size(); ++i){
		if(color[G[v][i]] == c)
			return false;
		if(color[G[v][i]] == 0 && !dfs(G[v][i], -c))
			return false;
	}
	return true;
}

void solve(){
	for(int i = 1; i <= n; ++i){
		if(color[i] != 0){
			if(!dfs(i, color[i])){
				printf("NO\n");
				return ;
			}
		}
	}
	for(int i = 1; i <= n; ++i){
		if(color[i] == 0){
			if(G[i].size()==0){
				printf("NO\n");
				return ;
			}
			if(!dfs(i, 1)){
				printf("NO\n");
				return ;
			}
		}
	}
	printf("YES\n");
}

int main(){
	while(scanf("%d %d %d %d", &n, &m, &x, &y) != EOF){
		for(int i = 0; i <= MAXN; ++i){
			G[i].clear();
		}
		memset(color, 0, sizeof(color));
		for(int i = 0; i < m; ++i){
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		for(int i = 0; i < x; ++i){
			scanf("%d", &u);
			color[u] = 1;
		}
		for(int i = 0; i < y; ++i){
			scanf("%d", &v);
			color[v] = -1;
		}
		solve();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值