HDU 5971 二分图判定

Posted liylho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5971 二分图判定相关的知识,希望对你有一定的参考价值。

Wrestling Match

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


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
 
思路:现将已知身份的人染色,之后对每个人根据对应关系进行染色,若出现矛盾,或者有人没被染上(不联通),则输出NO,否则YES。
代码:
 1 #include<bits/stdc++.h>
 2 //#include<regex>
 3 #define db double
 4 #include<vector>
 5 #define ll long long
 6 #define vec vector<ll>
 7 #define Mt  vector<vec>
 8 #define ci(x) scanf("%d",&x)
 9 #define cd(x) scanf("%lf",&x)
10 #define cl(x) scanf("%lld",&x)
11 #define pi(x) printf("%d\n",x)
12 #define pd(x) printf("%f\n",x)
13 #define pl(x) printf("%lld\n",x)
14 #define MP make_pair
15 #define PB push_back
16 #define inf 0x3f3f3f3f3f3f3f3f
17 #define fr(i,a,b) for(int i=a;i<=b;i++)
18 const int N=1e3+5;
19 const int mod=1e9+7;
20 const int MOD=mod-1;
21 const db  eps=1e-18;
22 const db  PI=acos(-1.0);
23 using namespace std;
24 vector<int> g[N];
25 int n,m,x,y,ok;
26 int vis[N];
27 void dfs(int u)
28 {
29     int f=0;
30     if(vis[u]==-1) vis[u]=0,f=1;
31     for(int i=0;i<g[u].size();i++){
32         int v=g[u][i];
33         if(vis[v]==-1) vis[v]=1-vis[u],dfs(v);//染色
34         else if(vis[v]==vis[u]&&f==1) f=0,vis[u]=1-vis[v];//最多变换一次染色方式
35         else if(vis[v]==vis[u]) ok=0;//矛盾
36     }
37 }
38 int main()
39 {
40     while(scanf("%d%d%d%d",&n,&m,&x,&y)==4)
41     {
42         ok=1;
43         for(int i=1;i<=n;i++) g[i].clear();
44         memset(vis,-1, sizeof(vis));
45         while(m--)
46         {
47             int u,v;
48             ci(u),ci(v);
49             g[u].push_back(v);
50             g[v].push_back(u);
51         }
52         while(x--){
53             int u;ci(u);vis[u]=0;
54         }
55         while(y--){
56             int u;ci(u);vis[u]=1;
57         }
58         for(int i=1;i<=n;i++){
59             if(g[i].size()) dfs(i);
60         }
61         for(int i=1;i<=n;i++) if(vis[i]==-1) ok=0;//不联通
62         if(ok) puts("YES");
63         else puts("NO");
64     }
65     return 0;
66 }

 

 

以上是关于HDU 5971 二分图判定的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5971"Wrestling Match"(二分图染色)

HDU2444(二分图判定+最大匹配)

HDU 2444 The Accomodation of Students 二分图判定+最大匹配

hdu5285-wyh2000 and pupil-(染色法二分图判定)

HDU - 2444 The Accomodation of Studentsp[二分图判定,匈牙利算法]

二分图匹配