A Bug's Life(HD1829)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829

代码:

#include <stdio.h>
#include<memory.h>
using namespace std;
int father[2001],child[2001],sex[2001];
int findfather(int x)
{
  while(x!=father[x])
    x=father[x];
  return x;
}
void unitnode(int x,int y)
{
  x=findfather(x);
  y=findfather(y);
  if(x==y) return;//x和y是同一个虫子
  else if(child[x]==child[y])
  {
    child[x]++;
    father[y]=x;
  }
  else if(child[x]>child[y])
    father[y]=x;
  else
    father[x]=y;
}
int main()
{
  int num,bugnum,casenum,a,b;
  scanf("%d",&num);
  bool judge;
  for(int i=1;i<=num;i++)
  {
    judge=true;
    memset(father,0,sizeof(father));
    memset(child,0,sizeof(child));
    memset(sex,0,sizeof(sex));
    scanf("%d%d",&bugnum,&casenum);
    for(int j=0;j<bugnum;j++)
       father[j]=j;
    for(int j=0;j<casenum;j++)
    {
     scanf("%d%d",&a,&b);
     if(judge)
     {
       if(findfather(a)==findfather(b))
       judge=false;
       else
       {
       if(!sex[a])
         sex[a]=b;
       else
         unitnode(sex[a],b);
       if(!sex[b])
         sex[b]=a;
       else
         unitnode(sex[b],a);
         }
     }

    }
    if(!judge)
    printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);
    else
    printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);
  }
}

 

posted @ 2013-05-27 10:19  supersnow0622  Views(195)  Comments(0)    收藏  举报