2203: Good and Bad

 2203: Good and Bad


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K427126Standard
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值