zoukankan      html  css  js  c++  java
  • POJ 2492 并查集应用的扩展

    A Bug's Life
    Time Limit: 10000MS Memory Limit: 65536K
    Total Submissions: 28651 Accepted: 9331

    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

    /*********************************
         author   : Grant Yuan
         time     : 2014/10/5 11:29
         algorithm: 并查集应用的扩展
         source   : POJ 2492
    **********************************/
    #include<iostream>
    #include<cstdio>
    #define MAX 2007
    using namespace std;
    int parent[MAX],relation[MAX];
    int n,t,m;
    int find_parent(int k)
    {
        if(k==parent[k])
            return k;
        int t=parent[k];
        parent[k]=find_parent(parent[k]);
        relation[k]=(relation[k]+relation[t]+1)%2;
        return parent[k];
    }
    void unite(int a,int b)
    {
        int pa=find_parent(a);
        int pb=find_parent(b);
        parent[pa]=pb;
        relation[pa]=(relation[b]-relation[a])%2;
    }
    int main()
    {
        scanf("%d",&t);
        int ct=1;bool first=0;
        while(t--){
            bool ans=0;
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++)
                parent[i]=i,relation[i]=1;
            for(int i=1;i<=m;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                int pa=find_parent(a);
                int pb=find_parent(b);
                if(pa==pb){
                    if(relation[a]==relation[b]) ans=1;
                }
                else unite(a,b);
            }
            printf("Scenario #%d:
    ",ct++);
            if(ans) printf("Suspicious bugs found!
    
    ");
            else printf("No suspicious bugs found!
    
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    记一次centos7.2下用crontab执行定时任务的过程(初级)
    海外手机号码正则匹配
    装了wamp之后,80端口被占用解决办法
    newtonsoft动态修改JObject
    .net正则提取手机号码,并替换带有手机号码的a标签
    .vs目录有什么用?
    centos7安装nginx-1.13.6 新手入门,图文解析
    centos7安装kafka_2.11-1.0.0 新手入门
    centos7安装apache 新手入门 图文教程
    面向对象——案例练习(4)判断点是否在圆的内部
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4298009.html
Copyright © 2011-2022 走看看