A - Wrestling Match HDU - 5971

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

Hint

/*
* @Author: lyuc
* @Date: 2017-05-01 15:48:50
* @Last Modified by: lyuc
* @Last Modified time: 2017-05-01 20:33:47
*/
/**
* 题意:有n个人每个人只能是好人或者是坏人,给你n对人的关系,每对的中两个人的关系是对立的,一定有一个
* 是坏人一个是好人,并且给了 x个确定是好人的编号, y个确定是坏人的编号,现在让你判断,是否能将
* 所有人划分成两个阵营(有人的身份不能确定也不行)
*
* 思路:裸的二分染色,按照输入情况进行建边,然后按照输入的x,y进行染色判断如果有矛盾那一定是不行的,
* 最后在讲给出的条件进行染色,最后如果有身份不明的人就可以除去了
*/
#include <stdio.h>
#include <vector>
#include <string.h>
#include <queue>
#include <iostream>
using namespace std;
int n,m;
int x,y;
int a[],b[];
vector<int>edge[];
int vis[];
bool ok;
int str;
void bfs(int u,int flag){
queue<int>q;
q.push(u);
vis[u]=flag;
while(!q.empty()){
int tmp=q.front();
q.pop();
for(int i=;i<edge[tmp].size();i++){
int v=edge[tmp][i];
if(vis[v]==vis[tmp]){
ok=false;
return;
}
if(vis[v]==){
vis[v]=(-vis[tmp]);
q.push(v);
}
}
}
}
void init(){
ok=true;
memset(vis,,sizeof vis);
for(int i=;i<;i++){
edge[i].clear();
}
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d%d%d",&n,&m,&x,&y)!=EOF){
init();
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
edge[a[i]].push_back(b[i]);
edge[b[i]].push_back(a[i]);
}
for(int i=;i<x;i++){
scanf("%d",&str);
if(vis[str]==-){
ok=false;
}
bfs(str,);
}
for(int i=;i<y;i++){
scanf("%d",&str);
if(vis[str]==){
ok=false;
}
bfs(str,-);
}
for(int i=;i<m;i++){
if(vis[a[i]]==&&vis[b[i]]==)
bfs(a[i],);
}
for(int i=;i<=n;i++){
if(vis[i]==){
ok=false;
break;
}
}
printf(ok?"YES\n":"NO\n");
}
return ;
}
上一篇:【Effective Java】5、覆盖equals时总要覆盖hashcode


下一篇:C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出