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


  • 相关阅读:
    maven总结二: 常用标签及属性
    maven总结一: 常用命令
    SpringCloud集成Seata并使用Nacos做注册中心与配置中心
    Seata 分布式事务解决方案及特点
    mui 文件附件下载
    Mock学习记录
    postman 测试elasticsearch 导出json
    elasticsearch 整合spring cloud Alibaba nacos 配置文件
    Elasticsearch 多字段高亮分页查询 返回泛型List集合
    Elasticsearch 多字段高亮字段返回json
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254416.html
Copyright © 2011-2022 走看看