zoukankan      html  css  js  c++  java
  • 并查集--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.
     
    题目大意:有N个虫子,M个关系,判断这些虫子的关系(即性取向)是否正确
          按照样例输出。
    解题思路:很明显用并查集比较好处理
         处理时候有一个就是保存深度的数组ra[Maxn],0表示虫子是同性,1表示虫子是异性
     
    下面就闲话少叙,代码走起
     
    /*之前一直没搞懂为什么ra这个数组要取余运算原来是用0
        表示同类,1表示不同类,所以下面的21还有30行会取模*/
    #include<iostream>
    #include<cstdio>
    using namespace std;
    const int Maxn=2005;
    int pa[Maxn],ra[Maxn];//用来存储父节点还有关系
    void init(int n)
    {
        for(int i=1;i<=n;i++)
        {
            pa[i]=i;
            ra[i]=0;
        }
    }
    int find_set(int x)
    {
        int t=pa[x];
        if(pa[x]==x) return x;
        pa[x]=find_set(t);
        ra[x]=(ra[x]+ra[t])%2;
        return pa[x];
    }
    void union_set(int x,int y)
    {
        int fx=find_set(x);
        int fy=find_set(y);
        if(fx==fy)    return;
        pa[fx]=fy;
        ra[fx]=(ra[x]+1+ra[y])%2;
    }
    int main()
    {
        int T;
        int num,ncase;
        int x,y;
        scanf("%d",&T);
    //    {
            for(int i=1;i<=T;i++)
            {
                bool flag=true;
                scanf("%d%d",&num,&ncase);
                init(num);
                while(ncase--)
                {
                    scanf("%d%d",&x,&y);
                    if(find_set(x)==find_set(y))
                    {
                        if(ra[x]==ra[y])
                        {
                            flag=false;
                            continue;
                        }
                    }
                    else
                    {
                        union_set(x,y);
                    }
                }
            printf("Scenario #%d:
    ", i);
            if(flag) printf("No suspicious bugs found!
    ");
            else printf("Suspicious bugs found!
    ");
            printf("
    ");
            }
    //    }
        return 0;
    }
  • 相关阅读:
    C语言丨二维数组常用的4种表示方法
    据说程序员最怕命名!这个 6300 Star 的手册能帮上忙
    C语言基础丨(六)程序结构——分支(选择)结构【2】
    系统管理员应该知道的 20 条 Linux 命令!越早学会越好!
    憋不住了!这8 个 MySQL 陷阱,我不得不说一下...
    记录一次@Around使用不正确造成的StackOverflowError
    【java基础】为何e1==(e1=e2)为false
    《Java程序性能优化》subString()方法的内存泄露
    【爬虫】花瓣采集下载器
    SpringMVC容器加载流程总结
  • 原文地址:https://www.cnblogs.com/acmblog/p/9486698.html
Copyright © 2011-2022 走看看