0
点赞
收藏
分享

微信扫一扫

hdu 1829 A Bug's Life ( 并查集 )


A Bug's Life


Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10128    Accepted Submission(s): 3308



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


Hint

Huge input,scanf is recommended.



 题目分析:
并查集的一种应用,利用sex数组记录某个节点和它所在的集合树的根的关系,如果当前给出同属于一个集合的两个点,如果它俩已经被分配到一个集合中,那么需要判断他们和根节点的关系,然后利用亦或的性质就可以得到这两个节点的关系,如果他们关系是性别相同,那么与题意矛盾.
如果他们当前不在同一棵集合树中,那么就将他们加到同一棵集合树中,因为sex(u,x)^sex(v,y)^sex(u,v) == 1,所以 sex(u,v) = sex(u,x)^sex(v,y)^1 = !(sex(u,x)^sex(v,y))
那么这道题得解,注意并查集可以利用路径压缩优化

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 1000007

using namespace std;

int t,n,m;
int fa[MAX];
int sex[MAX];

void init ( int n )
{
    for ( int i = 0 ; i <= n ; i++ )
        fa[i] = i,sex[i] = 0;
}

int find ( int x , int &s )
{
    if ( x == fa[x] ) 
    {
        s = 0;
        return x;
    }
    else 
    {
        fa[x] = find (fa[x] , s );
        s = sex[x]^s;
        sex[x] = s;
        return fa[x];
    }
}

int main ( )
{
    int u , v;
    scanf ( "%d" , &t );
    int c = 1;
    while ( t-- )
    {
        scanf ( "%d%d" , &n , &m );
        init( n );
        bool flag = true;
        for ( int i = 0 ; i < m ; i++ )
        {
            scanf ( "%d%d" , &u , &v );
            if ( !flag ) continue;
            int state1 , state2;
            u = find( u , state1 );
            v = find( v , state2 );
            if ( u == v )
                flag = state1^state2;
            else
            {
                fa[u] = v;
                sex[u] = !(state1^state2);
            }
        }
        printf ( "Scenario #%d:\n" , c++ );
        if ( flag ) puts ("No suspicious bugs found!");
        else puts("Suspicious bugs found!");
        puts ( "" );
    }
}




举报

相关推荐

0 条评论