zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    BZOJ 1266: [AHOI2006]上学路线route
    重磅!阿里云Promtheus 正式免费公测
    解锁云原生 AI 技能|在 Kubernetes 上构建机器学习系统
    更新与发展 | Alibaba Cloud Linux 2 特性与开发细节揭秘
    《2019上半年DDoS攻击态势报告》发布:应用层攻击形势依然严峻,海量移动设备成新一代肉鸡
    《2019年上半年Web应用安全报告》发布:90%以上攻击流量来源于扫描器,IP身份不再可信
    并发模式与 RPS 模式之争,性能压测领域的星球大战
    互联网商城的上云改造之旅
    技术人具备“结构化思维”意味着什么?
    弘康人寿基于 RocketMQ 构建微服务边界总线的实践
  • 原文地址:https://www.cnblogs.com/smallhester/p/10331303.html
Copyright © 2011-2022 走看看