1. 程式人生 > >zcmu 1437 A Bug's Life(並查集)

zcmu 1437 A Bug's Life(並查集)

【題目】

1437: A Bug's Life

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 112  Solved: 49 [Submit][Status][Web Board]

Description

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. 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 1, and up to 2000) and the number of interactions (up to 5000) 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 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

Suspicious bugs found!
No suspicious bugs found!

【題意】

輸入一對戀人,判斷是否一定存在同性戀。

【思路】

用pre[]存父節點,用r[]存性別。初始化性別為0,假設輸入的戀人性別不同,如果父節點不同說明沒有出現過,未確定性別,則將其中一個改變性別;若父節點相同且性別相同,則說明一定存在同性戀。

【程式碼】

#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 
#include <cstdlib> 
#include <map> 
#include <list> 
#include <vector> 
#include <stack> 
#include <queue> 
#include <algorithm> 
#include <iostream> 
#define go(i,a,b) for(int i=a;i<=b;i++) 
#define og(i,a,b) for(int i=a;i>=b;i--) 
#define mem(a) memset(a,0,sizeof(a)) 
using namespace std; 
int pre[2005],r[2005],f; 
int find(int x) 
{ 
    if(x==pre[x]) return x; 
    int t=find(pre[x]); 
    r[x]=(r[x]+r[pre[x]])%2; 
    return pre[x]=t; 
} 
void join(int x,int y) 
{ 
    int fx=find(x),fy=find(y); 
    if(fx==fy) 
    { 
        if(r[x]==r[y]) f=1; 
        return; 
    } 
    pre[fx]=fy; 
    r[fx]=(r[x]+r[y]+1)%2; 
} 
main() 
{ 
    int t,n,m,a,b; 
    scanf("%d",&t); 
    while(t--) 
    { 
        f=0; 
        scanf("%d%d",&n,&m); 
        go(i,1,n) pre[i]=i,r[i]=0; 
        while(m--) 
        { 
            scanf("%d%d",&a,&b); 
            if(f) continue; 
            else join(a,b); 
        } 
        if(f) printf("Suspicious bugs found!\n"); 
        else printf("No suspicious bugs found!\n"); 
    } 
}