变异并查集2223: A Bug's Life

     


    StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
    stdin/stdout10s8192K38386Standard

    Background
    Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.

    Problem
    Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

    Input Specification

    The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

    Output Specification


    The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

    Sample Input

    2
    3 3
    1 2
    2 3
    1 3
    4 2
    1 2
    3 4
    

    Sample Output

    Scenario #1:
    Suspicious bugs found!
    
    Scenario #2:
    No suspicious bugs found!
    
    

     

    Problem Source: TUD Contest 2005, Darmstadt, Germany

     


    #include<stdio.h>
    #define mx 2000
    struct
    {
           int rank;
           int parent;
           int opposite;   //与自己不同性的某一虫子,初始化时为自己
    }bugs[mx];
    void init(int n)   //初始化,产生n个集合,每个集合一只虫
    {
         for(int i=0;i<n;i++)
         {
             bugs[i].rank=0;
             bugs[i].parent=i;
             bugs[i].opposite=i;
         }
    }
    int getfather(int x)   //寻找根节点,路径压缩
    {
         if(x!=bugs[x].parent)
           bugs[x].parent=getfather(bugs[x].parent);
         return bugs[x].parent;
    }
    void Union(int x,int y)     //合并相同集合,按秩合并
    {
         int a=getfather(x);
         int b=getfather(y);
         if(bugs[a].rank>bugs[b].rank)
             bugs[b].parent=a;
         else
             bugs[a].parent=b;
         if(bugs[x].rank==bugs[y].rank)
           bugs[y].rank++;
    }
    int main()
    {
         int ts;//测试数据的数量
         scanf("%d",&ts);
         int t=1;
         while(ts--)
         {
           int bugnum/*虫子数*/,pairs/*研究的对数*/;
           scanf("%d%d",&bugnum,&pairs);
           init(bugnum);
           int i,m,f,a,b;
           bool IsFound=false;
           for(i=0;i<pairs;i++)
           {
               scanf("%d%d",&m,&f);//m号虫,f号虫
               a=getfather(m-1);
               b=getfather(f-1);
               if(a==b)//两只虫在同一集合中则有同性恋虫的怀疑
                 IsFound=true;
               if(bugs[m-1].opposite!=m-1)//如果opposite中不是自己,则union(b,bugs[m-1].opposite
                 Union(b/*f-1*/,bugs[m-1].opposite);
               else//如果opposite是自己(既未被设置),则设为对方。
                 bugs[m-1].opposite=b;//f-1;
               if(bugs[f-1].opposite!=f-1)//同理
                 Union(a/*m-1*/,bugs[f-1].opposite);
               else
                 bugs[f-1].opposite=a;//m-1;
           }
          if(IsFound)
             printf("Scenario #%d:/nSuspicious bugs found!/n/n",t++);
           else
             printf("Scenario #%d:/nNo suspicious bugs found!/n/n",t++);
         }
         return 0;
    }

    • 0
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值