Wrestling Match
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2097 Accepted Submission(s): 756
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
思路:现将已知身份的人染色,之后对每个人根据对应关系进行染色,若出现矛盾,或者有人没被染上(不联通),则输出NO,否则YES。
代码:
#include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i,a,b) for(int i=a;i<=b;i++)
const int N=1e3+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db PI=acos(-1.0);
using namespace std;
vector<int> g[N];
int n,m,x,y,ok;
int vis[N];
void dfs(int u)
{
int f=;
if(vis[u]==-) vis[u]=,f=;
for(int i=;i<g[u].size();i++){
int v=g[u][i];
if(vis[v]==-) vis[v]=-vis[u],dfs(v);//染色
else if(vis[v]==vis[u]&&f==) f=,vis[u]=-vis[v];//最多变换一次染色方式
else if(vis[v]==vis[u]) ok=;//矛盾
}
}
int main()
{
while(scanf("%d%d%d%d",&n,&m,&x,&y)==)
{
ok=;
for(int i=;i<=n;i++) g[i].clear();
memset(vis,-, sizeof(vis));
while(m--)
{
int u,v;
ci(u),ci(v);
g[u].push_back(v);
g[v].push_back(u);
}
while(x--){
int u;ci(u);vis[u]=;
}
while(y--){
int u;ci(u);vis[u]=;
}
for(int i=;i<=n;i++){
if(g[i].size()) dfs(i);
}
for(int i=;i<=n;i++) if(vis[i]==-) ok=;//不联通
if(ok) puts("YES");
else puts("NO");
}
return ;
}