hdu 5971 Wrestling Match



Wrestling Match

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


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
 


#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<map>
#define ms(x) memset( (x),0,sizeof(x) );
using namespace std;
typedef long long int ll;
map<int,int>mp;
vector<int>v[1009];
int sum,f;
void init(){
	mp.clear();
	for(int i = 0;i < 1009;i++) v[i].clear();
	sum = f = 0;
}
void dfs(int n,int p){
	if( f ) return;
	for(int i = 0;i < v[n].size();i++){
		if( mp[ v[n][i] ] == p ) {
			f = 1;return;
		}
		if( mp[ v[n][i] ] == 0 ){
			mp[ v[n][i] ] = -p,sum++;
			dfs( v[n][i],-p );
		}
	}
}
int main()
{
//	freopen("E:\\workspace\\acm---C++\\acm\\1.txt","r",stdin);
	int n,m,x,y;
	while( scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF ){
		init();
		while(m--){
			int a,b;	scanf("%d%d",&a,&b);
			v[a].push_back(b),v[b].push_back(a);
		}
		for(int i = 0;i < x;i++){
			int a;	scanf("%d",&a);
			if( f ) continue;
			if( mp[a] == -1 ) { f = 1;continue; }
			if( mp[a] == 0 ) {
				mp[a] = 1,sum++;
				dfs(a,1);
			}
		}
		for(int i = 0;i < y;i++){	
			int a;	scanf("%d",&a);
			if( f ) continue;
			if( mp[a] == 1 ) { f = 1;continue; }
			if( mp[a] == 0 ){
				mp[a] = -1,sum++;
				dfs(a,-1);
			}
		}
		for(int i = 1;i <= n;i++){
			if( mp[i] == 0 ){
				if( v[ i ].size() == 0 ){ f = 1;break; }
				mp[i] = 1,sum++;
				dfs( mp[i],1 );
			}
		}
		if( n==sum && f == 0 ) puts("YES");
		else puts("NO");
	}
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章