HDU 1829 A Bug's Life

题目:链接

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

Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!

Hint

Huge input,scanf is recommended.

题意:

    有t组数据,共有n个虫子,m种虫子间的交互关系a,b,在给出的交互关系中找到可疑的虫子,即有同性倾向的虫子。

思路:

  • 种类并查集。正常情况下两两虫子互相交互且都为异性,也就是说把给出的交互关系当作告诉我们这两个虫子间是异性关系,如果一个虫子存在两种性别就说明发生冲突则存在可疑虫子。
  • 在给出的交互关系中首先判断两个虫子是不是具有同一根结点,若不是同一根结点则将其归并成一个根节点,表示两者间存在交互关系。
  • 若在新给出的交互关系中发现二者已存在交互关系,则说明存在可疑虫子。

代码:

 1 #include <iostream>
 2 #include <cstdio> 
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <cstring>
 6 using namespace std;
 7 
 8 #define MAXN 5010
 9 
10 int pre[MAXN];
11 
12 void init(int n)
13 {
14     for(int i = 1; i <= 2*n; i++)
15     {
16         pre[i] = i;                                            
17     }
18 }
19 
20 int find(int x)
21 {
22     if(x != pre[x])
23         pre[x] = find(pre[x]);
24         
25     return pre[x];
26 }
27 
28 void merge(int x, int y)
29 {
30     int fx = find(x);
31     int fy = find(y);
32     if(fx != fy)
33     {
34         pre[fx] = fy;
35     }
36 }
37  
38 int main()
39 {
40     int t, n, m;               
41     scanf("%d", &t);
42     for(int k = 1; k <= t; k++)
43     {
44         scanf("%d%d", &n, &m);
45         init(n);  
46         int a, b;
47         int sum = 0;
48         for(int i = 1; i <= m; i++)
49         {
50             scanf("%d%d", &a, &b);
51             if((find(a) == find(b)) || (find(a+n) == find(b+n)))
52                 sum++;
53             else    
54             {
55                 merge(a, b+n);
56                 merge(a+n, b);
57             }            
58         }
59         printf("Scenario #%d:\n", k);
60         if(!sum)     
61             printf("No suspicious bugs found!\n\n");
62         else
63             printf("Suspicious bugs found!\n\n");
64     }
65     return 0;
66  } 

总结:

  还没想明白为何要a+n,b+n,不放又会WA。

posted @ 2019-07-17 10:53  Anzer  阅读(124)  评论(0编辑  收藏  举报