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;
    }
  • 相关阅读:
    angularJS中的MVC思想?
    angularJs初体验,实现双向数据绑定!使用体会:比较爽
    原生JS去解析地址栏的链接?超好用的解决办法
    HDCMS多图字段的使用?
    sublime添加到鼠标右键打开文件的方法?
    Ajax做列表无限加载和Ajax做二级下拉选项
    Atitit.获取某个服务 网络邻居列表 解决方案
    Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理
    Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理
    Atitit.prototype-base class-based  基于“类” vs 基于“原型”
  • 原文地址:https://www.cnblogs.com/acmblog/p/9486698.html
Copyright © 2011-2022 走看看