zoukankan      html  css  js  c++  java
  • POJ2492 A Bug's Life[并查集]

    A Bug's Life
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 21679   Accepted: 7071

    Description

    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

    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

    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!

    Hint

    Huge input,scanf is recommended.

    Source

    TUD Programming Contest 2005, Darmstadt, Germany

    注意本题一定要单例输出

    code:

     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 #define MAXN 2001
    22 
    23 int father[MAXN];
    24 int kind[MAXN];
    25 int n,m;
    26 //---------------------------------------------核心----------------------------------------------
    27 void init()
    28 {
    29     for(int i=0;i<MAXN;i++)
    30     {
    31         father[i]=i;
    32         kind[i]=0;
    33     }
    34 }
    35 
    36 int findset(int n)
    37 {
    38     
    39     if(father[n]==n)
    40         return n;
    41     int t=findset(father[n]);
    42     kind[n]=(kind[n]+kind[father[n]])%2;      //关键
    43     father[n]=t;
    44     return father[n];
    45 }
    46 
    47 bool Union(int a,int b)
    48 {
    49     int x,y;
    50     x=findset(a);
    51     y=findset(b);
    52     if(x==y)
    53     {
    54         if(kind[a]==kind[b])
    55             return 1;
    56     }
    57     else
    58     {
    59         father[x]=y;
    60         kind[x]=(kind[a]+kind[b]+1)%2;           //关键
    61     }
    62     return 0;
    63 }
    64 //---------------------------------------------核心----------------------------------------------
    65 int main()
    66 {
    67     int i;
    68     int t;
    69     int a,b;
    70     int cnt=1;
    71     int flag;
    72     scanf("%d",&t);
    73     while(t--)
    74     {
    75         flag=0;
    76         init();
    77         scanf("%d%d",&n,&m);
    78         for(i=0;i<m;i++)
    79         {
    80             scanf("%d%d",&a,&b);
    81             if(Union(a,b))
    82                 flag=1;
    83         }
    84         if(flag)
    85             printf("Scenario #%d:\nSuspicious bugs found!\n\n",cnt++);
    86         else
    87             printf("Scenario #%d:\nNo suspicious bugs found!\n\n",cnt++);
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    如何利用python制作微信好友头像照片墙?
    机器学习入门路线和资源
    突然“被辞职”的时候,原来可以拿到这么多钱!
    一个致命的 Redis 命令,导致公司损失 400 万
    程序员:想知道你每天按了多少次键盘吗?
    想了解真实的中国历史吗?建议看看这10部历史纪录片,受益终生!
    SpringBlade 2.0-RC3 发布,全新的微服务开发平台
    Syncd-开源自动化部署工具
    学习Spring Boot看这两个开源项目就够了!非得值得收藏的资源
    大型视频直播平台架构由浅入深详细讲解
  • 原文地址:https://www.cnblogs.com/XBWer/p/2630858.html
Copyright © 2011-2022 走看看