zoukankan      html  css  js  c++  java
  • poj2492--A Bug's Life(并查集变形)

    A Bug's Life
    Time Limit: 10000MS   Memory Limit: 65536K
    Total Submissions: 28703   Accepted: 9350

    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!

    Hint

    Huge input,scanf is recommended.

    Source

    TUD Programming Contest 2005, Darmstadt, Germany
    题目大意:给出n条虫子。m个配对(男<->女)的情况。问数据有没有错误(出现同性配对)?
    也就是说每一个给出的ab在不同的阵营,假设查询出现了在同一阵营就出现了错误。
    c数组记录属于某一个阵营。d数组记录它所属的阵营的敌对阵营。
    对每一组数据出现后。更新两个数组,注意:
    假设a属于x,b属于y,那么应该讲d[y]归并到x中,y归并到d[x]中
     
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int c[2100] , d[2100] ;
    int find1(int a)
    {
        if( c[a] != a )
        {
            c[a] = find1(c[a]) ;
            d[a] = d[ c[a] ] ;
        }
        return c[a] ;
    }
    int main()
    {
        int t , tt , i , j , n , m , a , b , flag ;
        scanf("%d", &t);
        for(tt = 1 ; tt <= t ; tt++)
        {
            scanf("%d %d", &n, &m);
            for(i = 1 ; i <= n ; i++)
                c[i] = i ;
            memset(d,-1,sizeof(d));
            flag = 0 ;
            while(m--)
            {
                scanf("%d %d", &a, &b);
                if( flag ) continue ;
                int x , y ;
                x = find1(a) ; y = find1(b) ;
                if(x == y)
                    flag = 1 ;
                else if( d[x] == -1 && d[y] == -1 )
                {
                    d[x] = y ; d[y] = x ;
                }
                else if( d[x] != -1 )
                {
                    c[y] = d[x] ;
                    if( d[y] != -1 )
                    {
                        int xx = find1(d[y]);
                        c[xx] = x ;
                        d[xx] = y ;
                    }
                    d[y] = x ;
                }
                else
                {
                    c[x] = d[y] ;
                    if( d[x] != -1 )
                    {
                        int yy = find1(d[x]);
                        c[yy] = y ;
                        d[yy] = x ;
                    }
                    d[x] = y ;
                }
            }
            printf("Scenario #%d:
    ", tt);
            if( flag )
                printf("Suspicious bugs found!
    
    ");
            else
                printf("No suspicious bugs found!
    
    ");
        }
        return 0;
    }
    

  • 相关阅读:
    ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现
    WEB API 中HTTP的get、post、put,delete 请求方式
    SQLServer中的事务与锁
    C#迭代器
    C#:异步编程和线程的使用(.NET 4.5 ),异步方法改为同步执行
    SD卡WAV音乐播放器(quartus11.0)(FAT32)(DE2-115)
    Xilinx IP核的根目录地址,有datasheet 和仿真相关的资料
    quartusii 使用ModelSim do文件实现仿真(Verilog)
    怎样用modelsim做后仿真
    ModelSim之tcl自动化仿真
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6955163.html
Copyright © 2011-2022 走看看