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

    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 33833   Accepted: 11078

    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

    带偏移量的并查集。

    两个集合并时,要注意处理dis数组,使点之间的关系不出错

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 using namespace std;
     7 const int mxn=20000;
     8 int fa[mxn];
     9 int dis[mxn];
    10 int n,m;
    11 bool flag;
    12 int find(int x){
    13     if(fa[x]==x){
    14         return x;
    15     }
    16     int newfx=find(fa[x]);
    17     dis[x]=(dis[x]+dis[fa[x]])%2;
    18     return fa[x]=newfx;
    19 }
    20 bool pd(int a,int b){
    21     int u=find(a),v=find(b);
    22     if(u==v){
    23         if(dis[a]!=(dis[b])^1){
    24             flag=1;
    25             return 0;
    26         }
    27     }
    28     else{
    29         fa[u]=v;
    30         dis[u]=((dis[a]-dis[b])^1);
    31     }
    32     return 1;
    33 }
    34 void init(int x){
    35     for(int i=1;i<=x;i++)fa[i]=i,dis[i]=0;
    36 }
    37 int main(){
    38     int T;
    39     scanf("%d",&T);
    40     for(int ro=1;ro<=T;ro++){
    41         printf("Scenario #%d:
    ",ro);
    42         scanf("%d%d",&n,&m);
    43         init(n);
    44         int i,j;
    45         int x,y;
    46         flag=0;
    47         for(i=1;i<=m;i++){
    48             scanf("%d%d",&x,&y);
    49             pd(x,y);
    50         }
    51         if(flag)printf("Suspicious bugs found!
    ");
    52         else printf("No suspicious bugs found!
    ");
    53         printf("
    ");
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    如何成为伟大的程序员
    程序员如何增加收入
    一个阿里巴巴码农的六年回眸
    效仿盖茨:PPstream创始人的心路历程
    程序员的工作环境与效率
    软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
    技术人员创业后就不再适合继续编码了
    互联网行业持续交付的经验
    11 款用于优化、分析源代码的Java工具
    常用 Java 静态代码分析工具的分析与比较
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5652743.html
Copyright © 2011-2022 走看看