Wrestling Match hdu-5971(思惟+STL)

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".
InputInput 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.OutputIf 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我的劃分紅好人和壞人兩個部分??

Input:
n,m,X,Y;接下來輸入m行,每行輸入兩個數a和b,表示a和b之間進行了一場比賽。
Output:
若是可以把n我的分紅好人和壞人兩部分就輸出YES;不然,輸出NO。
ios

思路:首先當a和b之間有比賽則說明a和b是對立的(即:若a好,則b壞;若a壞,則b好),因此凡是與a進行比賽的都是a的對立面。能夠用vector容器進行統計a的對立都有哪些,進而把全部比賽人的對立都統計徹底。

而後在輸入好和壞的時候,分別這我的的對立(即vector[x]裏的人)進行定義。若是是好(flag[x]=1),則vector[x]裏的人所有是壞的(flag[t]=2);反之,同理。
在flag標記的時候,若是有flag[t]=1,又須要把flag[t]定義成2時,則說明t不可以清楚的分爲好or壞。所以,ans=-1(ans的初始值爲0)。
判斷完已知的好壞以後,會發現有些人仍是不可以分清楚。對以前的比賽進行分類,若是比賽的兩我的都不能分清楚,則把第一我的定義爲1,而後對第一我的的全部對立進行定義;若是隻是第一我的沒有進行定義,就把第一我的定義成第二我的的對立,而後對第一我的的全部對立進行定義。
最後,對全部人的flag進行遍歷,查看是否有人的flag沒有被定義(是否有人根本沒有出現)。
而後根據ans,決定最後的輸出結果。ide

AC代碼:spa

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <algorithm>
  6 #include <map>
  7 #include <set>
  8 #include <stdlib.h>
  9 #include <stack>
 10 #include <vector>
 11 #include <cmath>
 12 #define ll long long
 13 using namespace std;
 14 vector<int>vec[1005];
 15 int mp[10005];
 16 int a[10005][2];
 17 int main()
 18 {
 19     int n,m,c,b,x,y;
 20     while(~scanf("%d%d%d%d",&n,&m,&c,&b))
 21     {
 22         memset(mp,0,sizeof(mp));
 23         memset(a,0,sizeof(a));
 24         int flag=0;
 25         for(int i=0; i<m; i++)
 26         {
 27             scanf("%d%d",&x,&y);
 28             a[i][0]=x;
 29             a[i][1]=y;
 30             vec[x].push_back(y);
 31             vec[y].push_back(x);
 32         }
 33         for(int i=0; i<c; i++)
 34         {
 35             scanf("%d",&x);
 36             if(mp[x]!=2)
 37                 mp[x]=1;
 38             else
 39                 flag=1;
 40             for(int j=0; j<vec[x].size(); j++)
 41             {
 42                 if(mp[vec[x][j]]!=1)
 43                     mp[vec[x][j]]=2;
 44                 else
 45                     flag=1;
 46             }
 47         }
 48         for(int i=0; i<b; i++)
 49         {
 50             scanf("%d",&x);
 51             if(mp[x]!=1)
 52                 mp[x]=2;
 53             else
 54                 flag=1;
 55             for(int j=0; j<vec[x].size(); j++)
 56             {
 57                 if(mp[vec[x][j]]!=2)
 58                     mp[vec[x][j]]=1;
 59                 else
 60                     flag=1;
 61             }
 62         }
 63         if(flag)
 64         {
 65             printf("NO\n");
 66         }
 67         else
 68         {
 69             for(int i=0; i<m; i++)
 70             {
 71                 if(mp[a[i][0]]==0&&mp[a[i][1]]==0)
 72                     mp[a[i][0]]=1;
 73                 else if(mp[a[i][0]]==0)
 74                 {
 75                     mp[a[i][0]]=mp[a[i][1]]%2+1;
 76                 }
 77                 x=a[i][0];
 78                 y=mp[x]%2+1;
 79                 for(int j=0; j<vec[a[i][0]].size(); j++)
 80                 {
 81                     if(mp[vec[x][j]]!=y%2+1)
 82                         mp[vec[x][j]]=y;
 83                     else
 84                         flag=1;
 85                 }
 86             }
 87             for(int i=1; i<=n; i++)
 88             {
 89                 if(mp[i]==0)
 90                 {
 91                     flag=1;
 92                     break;
 93                 }
 94             }
 95             if(flag)
 96                 printf("NO\n");
 97             else
 98                 printf("YES\n");
 99         }
100     }
101     return 0;
102 }
View Code
相關文章
相關標籤/搜索