【HDU 5971】二分染色

来源:互联网 发布:python有哪些图形库 编辑:程序博客网 时间:2024/05/18 16:14

传送门:

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

题意:

有n个人,编号为1-n, 已知X个人是good,Y个人是bad,m场比赛,每场比赛都有一个good和一个bad人结合起来,问这n个人是否能被分成两种人

思路:

其实就是判断是否为二分图,用染色法判断一下就可以了

代码:

#include <set>#include <map>#include <queue>#include <vector>#include <math.h>#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using  namespace  std;#define ff first#define ss second#define pb push_back#define ll long long#define mod 1000000007#define ull unsigned long long#define mst(ss,b) memset(ss,b,sizeof(ss));#define pl(x) cout << #x << "= " << x << endl;const int inf = 0x3f3f3f3f;const int N = 1e5+5;int n, m, x, y;vector<int>E[N];bool vis[N];int col[N];bool dfs(int u, int c){ // 二分染色    if(col[u] != -1 || vis[u]){//注意两个条件满足其一        if(c == col[u])return 1;        else return 0;    }    col[u] = c;    vis[u] = 1;    for(auto v : E[u]){        if(!dfs(v, 1-c))return 0;    }    return 1;}int  main(){    while(~scanf("%d%d%d%d", &n, &m, &x, &y)){        for(int i=0; i<N; i++)E[i].clear();        for(int i=0; i<N; i++)vis[i] = 0;        for(int i=0; i<N; i++)col[i] = -1;        for(int i=1; i<=m; i++){            int u, v;            scanf("%d%d", &u, &v);            E[u].pb(v);            E[v].pb(u);        }        for(int i=1; i<=x; i++){            int xx;            scanf("%d", &xx);            col[xx] = 1;        }        for(int i=1; i<=y; i++){            int xx;            scanf("%d", &xx);            col[xx] = 0;        }        bool fg = 1;        for(int i=1; i<=n; i++){ //先对已知进行染色            if(col[i] != -1){                int c = col[i];                col[i] = -1;                fg = fg&dfs(i, c);            }        }        if(!fg)puts("NO");        else{            for(int i=1; i<=n; i++){ //再对剩下的染色                if(!vis[i]){                    if(!E[i].size()){ //防止有孤立的点                        fg = 0;break;                    }                    fg = fg&dfs(i, 0);                }            }            if(!fg)puts("NO");            else puts("YES");        }    }    return 0;}

描述:

Wrestling Match

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

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亚洲区大连站-重现赛(感谢大连海事大学)

Recommend
wange2014

0 0
原创粉丝点击