2203: Good and Bad
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 3s | 8192K | 427 | 126 | Standard |
There are two types of professional wrestlers: "good guys" and "bad guys." Between any pair of professional wrestlers, there may or may not be a rivalry. Supose we have N(1 < N < 100) professional wrestlers and we have a list of R(R < 500) pairs of wrestlers for which there are rivalries. Can you determines whether it is possible to designate some of the wrestlers as good guys and the remainder as bad guys such that each rivalry is between a good guy and a bad guy.
Input
The input will consist several tests. The first line of each test there are two integers N, R, following R lines describe the a rivalry which consist two integers a and b which indicate that there is a rivalry between a and b.Output
If the designateing is possible you output "Yes" or "No" if not.Sample Input
4 4 1 2 2 4 3 1 4 3 3 3 1 2 2 3 3 1
Sample Output
Yes No
#include<stdio.h>
int fath[2005],num[2005],opp[2005];
int find(int x)
{
return fath[x]==x?x:fath[x]=find(fath[x]);
}
void uion(int x,int y)
{
x=find(x);
y=find(y);
if(x==y) return ;
if(num[x]>=num[y])
{
fath[y]=x;
num[x]+=num[y];
}
else
{
fath[x]=y;
num[y]+=num[x];
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
int i;
for(i=1;i<=n;i++) fath[i]=i,num[i]=1,opp[i]=i;
int flag=0;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
if(flag) continue;
if(find(x)==find(y)) flag=1;
if(opp[x]==x) opp[x]=y;
else uion(opp[x],y);
if(opp[y]==y) opp[y]=x;
else uion(opp[y],x);
}
if(flag) printf("No/n");
else printf("Yes/n");
}
return 0;
}