nyoj 209 + poj 2492 A Bug's Life (and look up)

A Bug's Life

Time Limit: 1000 ms | Memory Limit: 65535 KB
Difficulty: 4
 
describe
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.
 
enter
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 10000) 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!

1  /* *
 2 The main idea of ​​the      title:
 3.          Determine whether the two bugs (bed bugs) entered are gay. The professor thinks that the bugs he judged are of the opposite sex. Your task is to see whether the judgment of professor
 4          is true.
5          ①. If there are two bugs of the same sex, then output Suspicious bugs found! (find suspicious (same-sex) bugs)
 6          ②. If *bug (all bugs) are not of the same sex, finally output no Suspicious bugs found! ( No suspicious (same-sex) bed bugs were found)
 7  
8 Problem-      solving algorithm: and check the set
 9  
10      idea:
 11          1. Put all the bed bugs of the same sex together.
12              1.1. If the input data are in the same set, it means that the pair of bed bugs is suspicious.
 13              1.2. If the input data is not in the same set, it means that they are still in the opposite sex relationship.
 14                  1.2.1. At the same time, we assume that the data a and The data a+n is the opposite sex relationship
 15                 1.2.1. If a and b are also of opposite sex, then a+n and b are of the same sex (in the same set)
 16  
17      PS: Actually, this is a template question, as long as it is this relationship:
 18          < There are only two cases of data, the state of the latter data will be affected by the previous data, and judge whether the newly inserted data is legal>
 19  * */

Core template:

1  void my_init( int n) // initialization of data 
2  {
 3      for ( int i = 0 ; i <= n; ++ i)
 4          pre[i] = i;
 5  }
 6  int my_find( int x) // find its root node 
7  {
 8      int n = x;
 9      while (n != pre[n])
 10          n = pre[n];
 11      int i = x, j;
 12      while(pre[i] != n) // path compression 
13      {
 14          j = pre[i];
 15          pre[i] = n;
 16          i = j
 17      }
 18      return n;
 19  }
 20  void my_join( int a, int b) // Add data that is not the same root node 
21  {
 22      int n1 = my_find(a), n2 = my_find(b);
 23      if (n1 != n2)
 24          pre[n1] = n2;
 25  }
 26 bool my_judge( int a, int b) // judging whether they are in the same set 
27  {
 28      int n1 = my_find(a), n2 = my_find(b);
 29      if (n1 != n2)    return  true ;
 30      return  false ;
 31 }

C/C++ code implementation (AC):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 
 6 using namespace std;
 7 
 8 int T, n, m, pre[20005], flag, cnt;
 9 
10 void my_init()
11 {
12     int nn = n + n;
13     for (int i = 0; i <= nn; ++ i)
14         pre[i] = i;
15     return ;
16 }
17 
18 int my_find(int x)
19 {
20     int n = x;
21     while (n != pre[n])
22         n = pre[n];
23     int i = x, j;
24     while (pre[i] != n)
25     {
26         j = pre[i];
27         pre[i] = n;
28         i = j;
29     }
30     return n;
31 }
32 
33 bool judge(int a, int b)
34 {
35     int n1 = my_find(a), n2 = my_find(b);
36     if (n1 != n2) return true;
37     return false;
38 }
39 
40 void my_join(int a, int b)
41 {
42     int n1 = my_find(a), n2 = my_find(b);
43     if (n1 != n2) pre[n1] = n2;
44     return ;
45 }
46 
47 int main()
48 {
49     cnt = 1;
50     scanf("%d", &T);
51     for(int i = 0; i < T; ++ i)
52     {
53         flag = 0;
54         scanf("%d%d", &n, &m);
55         my_init();
56         while (m --)
57         {
58             int a, b;
59             scanf("%d%d", &a, &b);
60             if (flag) continue;
61 
62             if (judge(a, b) || judge(a + n, b + n))
63             {
64                 my_join(a, b + n);
65                 my_join(a + n, b);
66             }
67             else
68                 flag = 1;
69         }
70 
71         if (i != 0) puts("");
72         if (flag)
73             printf("Scenario #%d:\nSuspicious bugs found!\n", cnt ++);
74         else
75             printf("Scenario #%d:\nNo suspicious bugs found!\n", cnt ++);
76 
77     }
78     return 0;
79 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324116&siteId=291194637