• 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 }
  • 相关阅读:
    PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
    PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了
    PAT甲题题解-1028. List Sorting (25)-水排序
    BZOJ 1492 货币兑换Cash
    Codeforces 276D Little Girl and Maximum XOR
    Codeforces 526E Transmitting Levels
    Codeforces 335B Palindrome
    BZOJ 2527 Meteors
    Codeforces 449D Jzzhu and Numbers
    FJ省队集训DAY4 T3
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5652743.html
Copyright © 2020-2023  润新知