Wrestling Match
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 449 Accepted Submission(s): 206
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
Recommend
题意:n个人,m场比赛,x个good player ,y个bad player,每场比赛必须由一个good player和bad player组成,问能否完全区分good player 和bad player。
思路:其实就是问能否构成一个二分图,直接用染色法就好了。这题数据感觉有点水,我考虑少了好多情况都能ac,最后情况没漏的代码如下:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
typedef long long LL;
using namespace std;
#define maxn 1005
#define ll l,mid,now<<1
#define rr mid+1,r,now<<1|1
#define lson l1,mid,l2,r2,now<<1
#define rson mid+1,r1,l2,r2,now<<1|1
int n,m,x,y,vis[maxn],head[maxn],edge[maxn];
int good[maxn],bad[maxn];
struct node{
int v,next;
}p[maxn*10*2];
bool dfs(int x){
for(int i=head[x];~i;i=p[i].next){
int next=p[i].v;
if(~vis[next]){
if(vis[next]==vis[x])
return false;
}
else{
vis[next]=!vis[x];
if(!dfs(next))
return false;
}
}
return true;
}
int main(){
while(~scanf("%d%d%d%d",&n,&m,&x,&y)){
memset(vis,-1,sizeof(vis));
memset(head,-1,sizeof(head));
memset(edge,0,sizeof(edge));
bool jud=true;
for(int i=0;i<2*m;i++){
int u,v;
scanf("%d%d",&u,&v);
p[i].v=v;
p[i].next=head[u];
head[u]=i++;
p[i].v=u;
p[i].next=head[v];
head[v]=i;
edge[u]++;
edge[v]++;
}
for(int i=0;i<x;i++){
scanf("%d",&good[i]);
vis[good[i]]=1;
}
for(int i=0;i<y;i++){
scanf("%d",&bad[i]);
vis[bad[i]]=0;
}
for(int i=0;i<x;i++){
if(!dfs(good[i])){
jud=false;
break;
}
}
for(int i=0;i<y;i++){
if(!dfs(bad[i])){
jud=false;
break;
}
}
for(int i=1;i<=n;i++){
if(~vis[i])
continue;
vis[i]=1;
if(!dfs(i)||!edge[i]){
jud=false;
break;
}
}
if(jud)
printf("YES\n");
else
printf("NO\n");
}
}