zoukankan      html  css  js  c++  java
  • PoJ2492A Bug's Life并查集

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

    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!
    #include<iostream>
    #include<cstdio>
    using namespace std ;
    #define MAX 20010
    int fa[MAX] , ra[MAX] ;
    int find( int x )
    {
        if( x == fa[x] ) 
            return x ;
        int t = fa[x] ;
        fa[x] = find( fa[x] ) ;
        ra[x] = ( ra[x] + ra[t] ) % 2 ;
        return fa[x] ;
    }
    void Unint( int u , int v )
    {
        int x = find( u ) ;
        int y = find( v ) ;
        fa[y] = x ;// 可以用矢量想加相等记住公式
        ra[y] = ( 1 + ra[u] - ra[v] ) % 2 ;
    }
    int main()
    {
        int i , j , ok , n , m ;
        int u , v , xx ,yy , T , Case = 0 ;
        cin >> T ;
        while( T-- )
        {  
            cin >> n >> m ;
            for( i = 1; i <= n ;i++)
            {
                ra[i] = 0;
                fa[i] = i ;
            }
            ok = 0 ;
            while( m-- )
            {
                scanf( "%d%d" , &u , &v ) ;
                if( ok ) continue ;
                xx = find( u ) ;
                yy = find( v ) ;
                if( xx == yy )
                {
                    if( ra[u] == ra[v ])
                        ok = 1 ;// 如果在一颗树上而且和父亲节点的性别相同
                }
                else 
                {
                    Unint( u , v ) ;
                }
            }
            if( ok ) printf( "Scenario #%d:\nSuspicious bugs found!\n" , ++Case ) ;
            else printf( "Scenario #%d:\nNo suspicious bugs found!\n" , ++Case ) ;
            if( T != 0 ) printf( "\n" ) ;
        }
    }
  • 相关阅读:
    轨迹预测-运动递归函数
    Mandelbrot集合及其渲染
    如何检测一个圆在多个圆内?
    【转】三十分钟掌握STL
    【转】如何理解c和c++的复杂类型声明
    有1,2,3一直到n的无序数组,排序
    归并排序
    希尔排序
    快速排序
    冒泡排序
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3072545.html
Copyright © 2011-2022 走看看