• 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;
    }
    


  • 相关阅读:
    OC练习题
    如何将字符串@“ abc123.xyz789”倒置
    整数转换成字符串倒叙放在数组中遍历
    查找名字中有王的姓
    查询单词里包含的字符串
    OC7考核
    OC考核测试题
    OC6考核
    OC5考核
    KH8
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4298009.html
Copyright © 2020-2023  润新知