zoukankan      html  css  js  c++  java
  • 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!
    ***************************************************************************************************************************
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 using namespace std;
     7 int fa[2005];
     8 int com[2005];
     9 int i,j,k,n,m,cas;
    10 void init()
    11 {
    12     for(i=1;i<=2005;i++)
    13      fa[i]=i;
    14     memset(com,-1,sizeof(com));
    15 }
    16 int find(int x)
    17  {
    18      if(x==fa[x])
    19        return fa[x];
    20      return fa[x]=find(fa[x]);
    21  }
    22 void Unon(int x,int y)
    23  {
    24     int xt=find(x);
    25     int yt=find(y);
    26      if(xt!=yt)
    27       fa[xt]=yt;
    28  }
    29 int main()
    30 {
    31     scanf("%d",&cas);
    32     k=0;
    33     while(cas--)
    34     {
    35         ++k;
    36         init();
    37         int st,en;
    38         scanf("%d%d",&n,&m);
    39         int found=0;
    40         for(i=1;i<=m;i++)
    41         {
    42             scanf("%d%d",&st,&en);
    43             if(com[st]!=-1)
    44             {
    45                 if(com[en]!=-1)
    46                 {
    47                     if(find(st)==find(en))
    48                     {
    49                         found=1;
    50 
    51                     }
    52                     Unon(en,com[st]);
    53                     Unon(st,com[en]);
    54                      continue;
    55                 }
    56                 Unon(com[st],en);
    57                 com[en]=st;
    58             }
    59             else
    60             {
    61                 if(com[en]!=-1)
    62                 {
    63                     Unon(com[en],st);
    64                     com[st]=en;
    65                 }
    66                 else
    67                 {
    68                     com[st]=en;
    69                     com[en]=st;
    70                 }
    71             }
    72         }
    73         printf("Scenario #%d:
    ",k);
    74         if(found==1)
    75           printf("Suspicious bugs found!
    ");
    76         else
    77           printf("No suspicious bugs found!
    ");
    78         printf("
    ");
    79 
    80     }
    81     return 0;
    82 
    83 }
    View Code
     
  • 相关阅读:
    对象无法注册到Spring容器中,手动从spring容器中拿到我们需要的对象
    sping,springMVC @Component 注解的对象都是单例模式,变量不能全局
    java读取项目路径下的中文文件乱码问题
    springboot集成mongoDB 异常认证
    观察者模式
    MongoDB学习笔记03
    MongoDB学习笔记02
    ajax参数中出现空格
    web并发模型
    MongoDB shell
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3375114.html
Copyright © 2011-2022 走看看