zoukankan      html  css  js  c++  java
  • 5.1.1 A Bug's Life

    A Bug's Life

    Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 425 Accepted Submission(s): 159

    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 /*
     2 ID:wuhuaju2
     3 PROG:
     4 LANG:C++
     5 */
     6 #include <cstdio>
     7 #include <iostream>
     8 #include <cstdlib>
     9 #include <algorithm>
    10 #include <cstring>
    11 #include <string>
    12 using namespace std;
    13 
    14 const int maxn=2010;
    15 int f[maxn],r[maxn];
    16 int T,n,m,f1,f2,x,y;
    17 bool flag=false;
    18 
    19 void close()
    20 {
    21     exit(0);
    22 }
    23 
    24 void work()
    25 {
    26 }
    27 
    28 int find(int k)
    29 {
    30     if (k==f[k])
    31         return k;
    32     int t=f[k];
    33     f[k]=find(f[k]);
    34     r[k]=(r[k]+r[t]+1) % 2;
    35     return f[k];
    36 }
    37 
    38 void init ()
    39 {
    40    scanf("%d",&T);
    41     int cnt=0;
    42     while (T--)
    43     {
    44         flag=false;
    45         cnt++;
    46         scanf("%d %d",&n,&m);
    47         for (int i=0;i<=n;i++)
    48         {
    49             f[i]=i;
    50             r[i]=1;
    51         }
    52         for (int i=1;i<=m;i++)
    53         {
    54             scanf("%d %d",&x,&y);
    55             f1=find(x);
    56             f2=find(y);
    57                if (f1==f2 && r[x]==r[y])
    58                 {
    59                     flag=true;
    60             //        break;
    61                 }
    62                     f[f1]=f2;
    63             r[f1]=(r[x]+r[y])%2;
    64         }
    65     //    union_set();
    66    printf("Scenario #%d:\n",cnt);
    67     if (flag) printf("Suspicious bugs found!\n\n");
    68     else printf("No suspicious bugs found!\n\n");
    69     }
    70 }
    71 
    72 int main ()
    73 {
    74     init();
    75     work();
    76     close();
    77     return 0;
    78 }
  • 相关阅读:
    用Zend Studio12 导入在workspace中的项目
    PHP 统计中文字符串的长度
    jQuery判断checkbox是否选中的3种方法
    js,jquery获取下拉框选中的option
    HTML与XHTML的区别
    HTML头部
    HTML框架标签
    js的继承
    图片懒加载
    Http请求的gzip压缩
  • 原文地址:https://www.cnblogs.com/cssystem/p/2909215.html
Copyright © 2011-2022 走看看