zoukankan      html  css  js  c++  java
  • hdu 5971 :Wrestling Match(DFS染色)

    http://acm.hdu.edu.cn/showproblem.php?pid=5971

    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

    题意分析:

    有n个人和m场比赛,每个人都是好球员或者坏球员,每场比赛获胜的是好球员,失败的是坏球员,已知有x个好球员和y个好球员,问能不能把这n个人完全分为好球员和坏球员,一个人只能有一个称号。

    解题思路:

    DFS染色,由于不是连通图,所以对每一个好球员和每一个坏球员染色,把与之比赛的人染成另一种颜色,之后还要对没有访问过的球员染色,期间如果遇到染色颜色不同的既可以结束, 如果一个人没有发生比赛,而且已知的好坏球员中都没有他,也是染色失败。

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #define N 1020
    using namespace std;
    vector<int>e[N];
    int n, m, temp, color[N], a[N], b[N];
    bool book[N];
    int dfs(int u)
    {
    	if(temp==0)
    		return 0;
    	book[u]=true;
    	for(int v=0; v<e[u].size(); v++){
    		int f=e[u][v];
    		if(color[f]==color[u]){
    			temp=0;
    			return 0;
    		}
    		if(color[u]==1)
    			color[f]=2;
    		else
    			color[f]=1;
    		if(book[f]==false)
    			dfs(f);
    	}
    	return 1;
    }
    void init()
    {
    	int x, y, u, v;
    	memset(color, 0, sizeof(color));
    	memset(book, false, sizeof(book));
    	scanf("%d%d", &x, &y);
    	for(int i=1; i<=n; i++)
    		e[i].clear();
    	while(m--)
    	{
    		scanf("%d%d", &u, &v);
    		e[u].push_back(v);
    		e[v].push_back(u);
    	}
    	for(int i=1; i<=x; i++){
    		scanf("%d", &a[i]);
    		color[a[i]]=1;
    	}
    	for(int i=1; i<=y; i++){
    		scanf("%d", &b[i]);
    		color[b[i]]=2;
    	}
    	temp=1;
    	for(int i=1; i<=x; i++)
    		if(!dfs(a[i]))
    		{
    			printf("NO
    ");
    			return ;	
    		} 
    	for(int i=1; i<=y; i++)
    		if(!dfs(a[i]))
    		{
    			printf("NO
    ");
    			return ;
    		}
    	for(int i=1; i<=n; i++)
    	{
    		if(!color[i]){
    			if(!e[i].size())
    			{
    				printf("NO
    ");
    				return ;
    			}
    			color[i]=1;
    			if(!dfs(i))
    			{
    				printf("NO
    ");
    				return ;
    			}
    		}
    	}
    	printf("YES
    ");
    }
    int main()
    {
    	while(scanf("%d%d", &n, &m)!=EOF){
    		init();
    	}
    	return 0;
    }
  • 相关阅读:
    ASP.NET中彩票项目中的计算复式投注的注数的方法
    移除http响应中的多余的头(X-AspNet-Version,Server等)
    获取枚举类型的描述description
    支付宝支付后回调通知中responseTxt=true isSign=False可能的问题
    《JAVA与模式》之门面模式
    《JAVA与模式》之组合模式
    《JAVA与模式》之参考资料
    《JAVA与模式》之有感
    从桥接模式与策略模式谈起(转载)
    《JAVA与模式》之桥接模式
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852510.html
Copyright © 2011-2022 走看看