zoukankan      html  css  js  c++  java
  • poj2492 (并查集,两个种类)

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

    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.
     
     
    分析:题目意思是给定n只虫子,不同性别的可以在一起,同性的不能在一起,
    给你m对虫子,判断中间有没有同性在一起的。
    难点在于如果当前两个虫子a,b还没有确定性别时,无法判断a=雌,b=雄和a=雄,b=雌哪个正确。
    可以把第i个虫子分为两个点,即i跟i+N分别代表两种性别,
    让a和b+N合并,b和a+N合并,最后判断a和a+N是否在同一集合内,b和b+N是否在同一集合内。
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int N;
    int pre[10000];
    void init()
    {
        for(int i=0;i<=2*N;i++)
        pre[i]=i;
    }
    int find(int x)
    {
        if(x!=pre[x])
        pre[x]=find(pre[x]);
        return pre[x];
    }
    void unit(int x,int y)
    {
        int rootx=find(x),rooty=find(y);
        if(rootx<rooty)
            pre[rooty]=pre[rootx];
        else pre[rootx]=pre[rooty];
    }
    bool judge(int x,int y)
    {
        int rootx=find(x),rooty=find(y);
        if(rootx==rooty)
        return false;
        return true;
    }
    
    int main()
    {
        int T,M,cas=0;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&N,&M);
            init();
            int flag=0;
            while(M--)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                if(judge(x,y)||judge(x+N,y+N))
                {
                    unit(x,y+N);//异性合并 
                    unit(x+N,y);
                }
                else flag=1;
            }
            if(!flag) printf("Scenario #%d:
    No suspicious bugs found!
    ",++cas);
            else printf("Scenario #%d:
    Suspicious bugs found!
    ",++cas);
            printf("
    ");
        }
        
        return 0;
    }
    View Code
     
     
  • 相关阅读:
    Redis(八):spring data redis 理解
    RPC服务框架dubbo(六):Consumer搭建过程
    Redis(七):Jedis简介和集群
    RPC服务框架dubbo(四):Dubbo中Provider搭建
    RPC服务框架dubbo(三):Dubbo支持的协议
    RPC服务框架dubbo(二):dubbo支持的注册中心
    RPC服务框架dubbo(一):简介和原理解析
    Java数据结构和算法(一):简介
    Golang gRPC实践 连载五 拦截器 Interceptor
    Go 1.8 http graceful 体验
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9734160.html
Copyright © 2011-2022 走看看