1. 程式人生 > >poj2492--A Bug's Life(並查集變形)

poj2492--A Bug's Life(並查集變形)

esp pst algorithm table out from nsis with bug else if

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:\n", tt);
        if( flag )
            printf("Suspicious bugs found!\n\n");
        else
            printf("No suspicious bugs found!\n\n");
    }
    return 0;
}

poj2492--A Bug&#39;s Life(並查集變形)