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 }
  • 相关阅读:
    maven 设置日志级别
    浏览器工作原理:浅析浏览器中的页面
    浏览器工作原理:浅析浏览器中的页面
    解决uniapp的websocket连接在web和安卓正常,iOS连接不上的问题
    浏览器工作原理:浅析页面循环系统
    浏览器工作原理:浅析页面循环系统
    浏览器工作原理:浅析页面循环系统
    浅析如何使用WebSocket、SockJS、STOMP实现消息实时通讯功能:websocket/SocketJS/Stomp是什么及三者的关系、stomp协议格式、如何开启stomp、如何处理客服端发送的stomp、如何发消息给客服端、如何在任何地方发消息、如何给目标或指定用户发消息
    解决sockjs、stomp在uni-app端使用的坑
    浏览器工作原理:浅析页面循环系统
  • 原文地址:https://www.cnblogs.com/XBWer/p/2630858.html
Copyright © 2011-2022 走看看