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;
    }
  • 相关阅读:
    Uva 11205 The broken pedometer
    Uva 331 Mapping the Swaps
    vs2005里取得offsetHeight,clientHeight,scrollHeight 三个属性值全都一样的问题
    网页根据自身高度动态调整所在iframe的高度
    一个简单的实现tab效果的demo
    iframe根据被嵌网页高度动态调整自身高度
    用window.open方法打开新窗口显示提示信息
    用C#在ASP.NET 2.0 的 TreeView 中查找某一节点
    解决 sharepoint 站点除了administrator 其他用户不能登陆的问题
    Python处理Excel,学会这十四个方法,工作量减少大半!
  • 原文地址:https://www.cnblogs.com/acmblog/p/9486698.html
Copyright © 2011-2022 走看看