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;
    }
  • 相关阅读:
    微信小程序,搜索结果关键词高亮 wxml不能动态识别html标签
    关于vue 全局loading方案
    element ui树样式问题
    Promise.all( ) 的使用
    关于form表单校验问题
    vue 异步改同步 获取结果 动态函数
    1.关于数据for循环不要用index作为key,2.面二次刷新404问题(空白) 的探讨 3. vue图片上传
    element ui点击切换皮肤
    关于element ui input、以及button样式不能覆盖的解决办法(登录页面)
    vue版本根据当前路由匹配到根父节点并且激活
  • 原文地址:https://www.cnblogs.com/acmblog/p/9486698.html
Copyright © 2011-2022 走看看