• A Bug's Life POJ


    A Bug's Life POJ - 2492 

    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.
     
    题意:有一些群体的昆虫,两两一对,每一对都是在一起的,问给出的里面有没有同性恋的昆虫
    题解:带权并查集,寻找与根节点的状态是否是一样的(是同性恋还是异性恋)
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<cstdlib>
    #include<vector>
    #include<string>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    const double PI = acos(-1.0);
    const int maxn =  5e4+10;
    const int mod = 1e9+7;
    
    struct node
    {
        int pre;
        int relation;  //0:同 1:异
    }p[maxn];
    
    int find(int x)
    {
        int temp;
        if(x == p[x].pre)
            return x;
        temp = p[x].pre;
        p[x].pre = find(temp);
        p[x].relation = (p[x].relation + p[temp].relation + 1) % 2;
        return p[x].pre;
    }
    void combine(int x,int y)
    {
        int xx = find(x);
        int yy = find(y);
        if(xx != yy)
        {
            p[xx].pre = yy;
            p[xx].relation = (p[y].relation - p[x].relation) % 2;
        }
    }
    int main()
    {
       int t;
       scanf("%d",&t);
       int ca=1;
       while(t--)
       {
           int n,m;
           int flag = 0;
           scanf("%d%d",&n,&m);
           for(int i=1;i<=n;i++)
           {
               p[i].pre = i;
               p[i].relation = 1;
           }
           for(int i=0;i<m;i++)
           {
               int a,b;
               scanf("%d %d",&a,&b);
               if(find(a) == find(b))
               {
                   if(p[a].relation == p[b].relation)
                       flag = 1;
               }
               else
                   combine(a,b);
           }
           if(flag)
                printf("Scenario #%d:
    Suspicious bugs found!
    
    ",ca++);
           else
               printf("Scenario #%d:
    No suspicious bugs found!
    
    ",ca++);
       }
    }
    View Code
  • 相关阅读:
    python 文件 笔记
    python 模块、包 笔记
    类、对象
    python 函数 笔记
    测试价值体现
    断舍离-笔记2
    Happy 2006 POJ
    Triangle War POJ
    Complete the sequence! POJ
    放苹果 POJ
  • 原文地址:https://www.cnblogs.com/smallhester/p/10331303.html
Copyright © 2020-2023  润新知