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
相关文章
相关标签/搜索