• 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" ) ;
        }
    }
  • 相关阅读:
    github中,一些涉及到用户配置的文件怎么处理(比如数据库用户名/密码)?
    XMLHttpRequest cannot load file浏览器无法异步加载本地file文件
    HTML小技巧
    HTML实体符号代码速查表
    宽度不固定,水平居中
    CSS实现背景透明,文字不透明
    window.inner 兼容IE8
    div横排放置对齐问题;block,inline,inline-block区别
    IE兼容CSS3圆角border-radius的方法(同时兼容box-shadow,text-shadow)
    js 值和引用
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3072545.html
Copyright © 2020-2023  润新知