天天看點

pku2492 并查集

題目來源:http://poj.org/problem?id=2492

題目分類:并查集

此題心得:學用并查集的關系判斷

時間:2011-7-21

A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 17804 Accepted: 5768

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page    Go Back   To top

All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di

Any problem, Please Contact Administrator

題意與分析

題意:題目告訴你人的個數,告訴你m個兩兩相戀的關系,問這個關系網出來之後會不會有同志出現(總所周知隻有兩種性别)。

分析:簡單的并查集隻能處理集合的合并,而現在這個題目似乎是要解決集合的拆散。。。也就是說是要維護一種關系狀态。因為隻有兩種性别,是以要充分利用好對2取模。也就是要重新開一個數組來存某個節點和根節點的關系,然後通過與根節點的關系就可以來判斷兩兩之間的關系了。。。

源代碼

·                //pku 2492      
·                #include<iostream>      
·                using namespace std;      
·                       
·                const int N=100010;      
·                int a[N], f[N], r[N], n, m, flag;      
·                       
·                int findf(int x)      
·                {      
·                      if(f[x]!=x)      
·                      {      
·                            int temp = f[x];      
·                            f[x] = findf(f[x]);      
·                            r[x] = (r[x]+r[temp])%2;      
·                      }      
·                      return f[x];      
·                }      
·                       
·                void unionf(int x, int y)      
·                {      
·                      int fx = findf(x);      
·                      int fy = findf(y);      
·                      if(fx==fy)      
·                      {      
·                            if(r[x]==r[y])      
·                                  flag = 0;      
·                            return;      
·                      }      
·                      f[fx] = fy;      
·                      r[fx] = (r[x]+r[y]+1)%2;      
·                }      
·                       
·                int main()      
·                {      
·                      int i, j, k, cas, cas1=1, x, y;      
·                      scanf("%d", &cas);      
·                      while(cas--)      
·                      {      
·                            scanf("%d%d", &n, &m);      
·                            for(i=1; i<=n; i++)      
·                            {      
·                                  f[i] = i;      
·                                  r[i] = 0;      
·                            }      
·                            flag = 1;      
·                            while(m--)      
·                            {      
·                                  scanf("%d%d", &x, &y);      
·                                  unionf(x, y);      
·                            }      
·                            printf("Scenario #%d:\n", cas1++);      
·                            if(flag==0)      
·                                  printf("Suspicious bugs found!\n");      
·                            else      
·                                  printf("No suspicious bugs found!\n");      
·                            printf("\n");      
·                      }      
·                            
·                      return 0;      
·                }      

繼續閱讀