Topic 209 topic Information running Results Discussion area
A Bug ' s lifeTime limit: Ms | Memory limit: 65535 KB Difficulty: 4 description
Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature, different genders and that they only interact with bugs of the opposite gender. In he experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their BAC Ks.
problem
Given A list of bugs interactions, decide whether the experiment supports his assumption of both 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 interaction S (up to 1000000) separated by a single space. In the following lines, each interaction was given in the form of both distinct bug numbers separated by a single space. Bugs is numbered consecutively starting from one. Outputs the output for every scenario are a line containing "scenario #i:", where I am the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment are consistent with he 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!
Test instructions
Input n Bug,bug between the interaction, the current hypothesis between the opposite sex is interaction, but need to verify, given these interaction pairs, determine whether to meet the assumptions
If male <-> female and female <-> male, meet condition, if present male <-> male or female <-> female, then assume not satisfied
Analysis:
Using relation[] array, where RELATION[X] represents x and parent node relationships, relation[x] = 0 for x and parent relationship for same sex, relation[x] = 1 for x and parent node relationship as opposite
About and checking the set, note two concepts: Merge by rank, path compression.
1. Merge by rank
Due to the fact that the collection is generally represented by a more efficient tree structure, the purpose of merging by rank is to prevent the generation of degraded trees (that is, tree-like lists),
Use an array to record the height of each element (as well as the number of children who record each element, depending on which can be convenient for solving the problem),
Then, at the time of merging, the small height is grafted to the top of the height, thus preventing the degradation of the tree.
2. Path compression
The other array records the ancestors of each element, which prevents a step-by-step recursive lookup of the father and thus the lost time. Since the collection of elements is only clear,
Instead of distinguishing the different sets we use to represent the elements (that is, the roots), so for each element we only need to save their ancestors, thus distinguishing the different collections.
Instead of using pure and check-set algorithms, we've expanded the problem
We did not use "1, by rank merger" (of course you can use, so you need to open an array)
We get the revelation from "1, merge by rank", the array that holds "rank" holds the relationship of the element relative to the parent node, and we can not take advantage of this relationship
(That is, different sets are distinguished by different rank values relative to the parent node), so that two collections can be combined into a single collection.
(Note: This code relation=0 represents the same gender as the parent node)
#include <cstdio> #include <cstring> using namespace std;
const int MAXN = 10000 + 10; int FATHER[MAXN]; Record parent node int RELATION[MAXN]; RELATION[X] records the relationship between X and parent node//where relation[x] = 0 means x and parent relationship is same sex, relation[x] = 1 for the opposite sex void init () {memset (relation, 0, size
of (relation));
for (int i = 0; i < MAXN; i++) father[i] = i;
} int Find (int x) {int temp = father[x];
if (temp = = x) return x;
FATHER[X] = Find (temp); RELATION[X] = (Relation[x] + relation[temp])% 2;
Modify the Relation[x] value from the old parent node and the new parent node relationship to return father[x];
} void Union (int a, int b) {int x = Find (a);
int y = Find (b);
if (x = = y) return;
Father[x] = y;
RELATION[X] = (Relation[a]-relation[b] + 1)% 2;
Return
} int main () {int T, CAS, n, M, A, B;
scanf ("%d", &t);
for (cas = 1; CAs <= t; cas++) {init ();
int flag = 1;
scanf ("%d%d", &n, &m);
for (int i = 0; i < m; i++) {scanf ("%d%d", &a, &b);
if (find (a) = = find (b)) {if (Relation[a]! = (Relation[b] + 1)% 2)//If you do not satisfy the heterosexual relationship, there is a contradiction
Flag = 0;
} else Union (A, b);
} if (flag) printf ("Scenario #%d:\nno suspicious Bugs found!\n\n", CAs);
else printf ("Scenario #%d:\nsuspicious bugs found!\n\n", CAs);
} return 0;
}