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 }
  • 相关阅读:
    DesignPattern系列__10单例模式
    DesignPattern系列__09设计模式概述
    DesignPattern系列__08UML相关知识
    DesignPattern系列__07合成复用原则
    DesignPattern系列__06迪米特原则
    爬取猫眼电影top100电影
    安卓微信对接H5微信支付出现“商家参数有误,请联系商家解决”的问题处理
    python 通过使用pandas的实现的Excel的批量转换CSV文件的处理
    输入一个字符串,判断字符串中最大对称字串的长度
    面向对象六大设计原则(转载)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5652743.html
Copyright © 2011-2022 走看看