zoukankan      html  css  js  c++  java
  • ACM1829并查集

    A Bug's Life

    Problem 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.
     1 #include<iostream>
     2 using namespace std;
     3 struct Pair
     4 {
     5     int from,conflict;
     6 };
     7 Pair *record;
     8 int find(int x)
     9 {
    10     if(x==record[x].from)
    11     return x;
    12     return record[x].from=find(record[x].from);
    13 }
    14 void link(int a,int b)
    15 {
    16     int fa=find(a);
    17     int fb=find(b);
    18     if(fa!=fb)
    19     record[fb].from=fa;
    20 }
    21 int main()
    22 {
    23     int t;
    24     int cnt=1;
    25     scanf("%d",&t);
    26     while(t--)
    27     {
    28         int n,m;
    29         bool flag=false;
    30         scanf("%d %d",&n,&m);
    31         record=new Pair[n+1];
    32         for(int i=1;i<=n;i++)
    33         {
    34             record[i].from=i;
    35             record[i].conflict=0;
    36         }
    37         int a,b;
    38         for(int i=1;i<=m;i++)
    39         {
    40             scanf("%d %d",&a,&b);
    41             int fa=find(a);
    42             int fb=find(b);
    43             if(fa==fb)flag=true;
    44             if(record[a].conflict!=0)
    45             {
    46                 link(b,record[a].conflict);
    47             }
    48             if(record[b].conflict!=0)
    49             {
    50                 link(a,record[b].conflict);
    51             }
    52             record[a].conflict=b;
    53             record[b].conflict=a;
    54         }
    55         cout<<"Scenario #"<<cnt<<":
    ";
    56         if(flag)cout<<"Suspicious bugs found!
    ";
    57         else cout<<"No suspicious bugs found!
    ";
    58         cout<<endl;
    59         cnt++;
    60     }
    61     return 0;
    62 }
    What I don't dare to say is I can't!
  • 相关阅读:
    JQuery直接调用asp.net后台WebMethod方法
    26个Jquery使用小技巧
    SQL 基本知识
    JQuery 基本知识
    ASP.NET CheckBoxList Operations with jQuery
    asp.net 锚点
    delphi 数据连接规范
    删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
    org.hibernate.QueryException: could not resolve property: address of:
    java.lang.ArithmeticException: / by zero
  • 原文地址:https://www.cnblogs.com/sytu/p/3872234.html
Copyright © 2011-2022 走看看