zoukankan      html  css  js  c++  java
  • HDU1829(种类并查集)

    ps:本来是想找个二分图判断的题来写,结果百度到这鬼题

    Problem 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!
     
    思路:建立一个N*2的并查集,其中1~N表示公,N+1~2*N表示母,与poj食物链异曲同工。
     

    #include<cstdio>

    using namespace std;

    #define max_n 2020*2

    int par[max_n];

    void init(int n)
    {
        for(int i=1;i<=n;i++)
        {
            par[i]=i;
        }
    }

    int find(int x)
    {
        if(par[x]==x) return x;
        else
        {
            return par[x]=find(par[x]);
        }
    }

    bool same(int x,int y)
    {
        return find(x)==find(y);
    }

    void unit(int x,int y)
    {
        int fx=find(x);
        int fy=find(y);
        if(fx==fy) return ;
        par[fx]=fy;
    }

    int main()
    {
        int t;
        scanf("%d",&t);
        for(int i=1;i<=t;i++)
        {
            int flag=0;
            int n,m;
            scanf("%d%d",&n,&m);
            init(n*2);

            for(int j=0;j<m;j++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                if(same(x,y)||same(x+n,y+n))
                {
                    printf("Scenario #%d: Suspicious bugs found! ",i);
                    flag=1;
                    break;
                }
                else
                {
                    unit(x,y+n);
                    unit(x+n,y);
                }
            }

            if(flag==0)
            {
                printf("Scenario #%d: No suspicious bugs found! ",i);
            }
        }
        return 0;
    }

  • 相关阅读:
    JQuery批量图片上传插件—Uploadify使用记录
    SQL2008 附加数据库提示 5120错误
    enter the full pathname for java.exe
    共享文件夹设置
    曾经运行该线程的应用程序域已卸载
    无法打开物理文件 。操作系统错误 5:"5(拒绝访问。)"。 消息 5120,级别 16,状态 101
    开篇一个码农,一个苦逼的码农生活经历
    POI 3.17 读取Excel(模板)、导出Excel
    Java微信支付开发之关闭订单
    Linux下安装 MySQL5.7.28
  • 原文地址:https://www.cnblogs.com/onlyli/p/6763418.html
Copyright © 2011-2022 走看看