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" ) ;
        }
    }
  • 相关阅读:
    js获取触发事件的元素
    js获取触发事件的元素
    jQuery动态显示和隐藏datagrid中的某一列的方法
    jQuery动态显示和隐藏datagrid中的某一列的方法
    在控制器“xxxx”上找不到与该请求匹配的操作
    在控制器“xxxx”上找不到与该请求匹配的操作
    配置virtualbox使得openstackn controller可以连接外网
    配置mysql远程访问
    fuel部完高可用环境后vip__public无法启动从而导致创建虚机时无法创建virtualInterface.
    VMWare/VirtualBox三种网络模式及NAT/host-only模式设置上网与主机互联通信
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/3072545.html
Copyright © 2011-2022 走看看