A - Wrestling Match HDU - 5971
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[10005],b[10005]; vector<int>edge[1005]; int vis[1005]; 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=0;i<edge[tmp].size();i++){ int v=edge[tmp][i]; if(vis[v]==vis[tmp]){ ok=false; return; } if(vis[v]==0){ vis[v]=(-vis[tmp]); q.push(v); } } } } void init(){ ok=true; memset(vis,0,sizeof vis); for(int i=0;i<1005;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=0;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=0;i<x;i++){ scanf("%d",&str); if(vis[str]==-1){ ok=false; } bfs(str,1); } for(int i=0;i<y;i++){ scanf("%d",&str); if(vis[str]==1){ ok=false; } bfs(str,-1); } for(int i=0;i<m;i++){ if(vis[a[i]]==0&&vis[b[i]]==0) bfs(a[i],1); } for(int i=1;i<=n;i++){ if(vis[i]==0){ ok=false; break; } } printf(ok?"YES\n":"NO\n"); } return 0; }