(Hdu step 5.1.1) A Bug & #39; s Life (ai, bi) indicates that ai and bi are not in the same heap, and there are several pairs of data to determine whether there is A bug)
Question:
A Bug's Life |
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission (s): 723 Accepted Submission (s): 277 |
|
Problem DescriptionBackground Extends sor 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 bugs 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. |
InputThe 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 suspicous bugs found! "If the experiment is consistent with his assumption about the bugs 'sexual behavior, or" Suspicious bugs found! "If your sor Hopper's assumption is definitely wrong. |
Sample Input23 31 22 31 34 21 23 4 |
Sample OutputScenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!HintHuge input,scanf is recommended. |
|
SourceTUD Programming Contest 2005, Darmstadt, Germany |
Recommendlinle |
Question:
Assume there are two groups of data.
1 2
1 3
The following information is obtained:
1) 1 and 2 can be paired
2) 1 and 3 can be paired
3) 2 and 3 belong to the same sex and cannot be paired. That is to say, data like "2 3" is not allowed.
Question Analysis:
And query sets. Simple question. Through the above "theme" understanding. In fact, when we can analyze a bunch of data in the meidu district "a B,
We need to do the following:
1) Determine whether the root nodes of a and B are the same. If it is the same, it does not match the meaning of the question (because a B Represents the root node of a and B)
2) determine whether a is from a group of the opposite sex. If not, create a group of the opposite sex for a, that is, sex [a] = B. Otherwise, add B to the opposite group of.
The Code is as follows:
/** A. cpp ** Created on: February 26, 2015 * Author: Administrator */# include
# Include
Using namespace std; const int maxn = 2001; int father [maxn]; // used to store parent-child relationships. for example, father [I] = I indicates that the father node of I is his own int sex [maxn]; // used to store the pairing relationship. sex [a] = B indicates that a can be paired with the bar/*** search for the root node of the tree where a is located */int find (int) {if (a = father [a]) {// if the father's day of a node is his own return; // It indicates that the root node has been found} return father [a] = find (father [a]); // otherwise, continue searching .} /*** Merge the two shards of a and B */void join (int a, int B) {int fa = find (); // find the root node int fb = find (B); if (fa! = Fb) {// if the root node of the tree where a is located is different from the root node of the tree where B is located, it is proved that a and B are in two different trees, father [fa] = fb; // merge the two shards of a and B}/*** initialize */void init () {int I; for (I = 1; I <maxn; ++ I) {// traverse all nodes father [I] = I; // point the father's Day point of each node to itself} int main () {int t; scanf ("% d", & t); int k; for (k = 1; k <= t; ++ k) {int n; int m; init (); memset (sex, 0, sizeof (sex); // initializes the pairing relationship. by default, no matching relationship exists between scanf ("% d", & n, & m); int I; bool flag = true; // indicates whether a bug flag exists during initialization. no bugfor (I = 1; I <= m; ++ I) {int by default A, B; scanf ("% d", & a, & B); if (flag = true) {// if no bug exists, then the new link int fa = find (a); // find the root node int fb = find (B) of ); // find the root node of B if (fa = fb) {// if a and B are in the same heap flag = false; // does not match the meaning of the question. mark flag as false. if the following code can be executed, the relationship between a and B is correct. if (sex [a] = 0) {// If a does not have a sex tree [a] = B; // create an heterosexual group for him. currently, this group only has one node B} else {// If a already has a heterosexual group join (sex [a], B ); // Add B to the opposite sex group} // perform the same operation on B if (sex [B] = 0) {sex [B] = ;} else {join (sex [B], a) ;}} printf ("Scenario # % D: \ n ", k); if (flag = true) {printf (" No suspicious bugs found! \ N ");} else {printf (" Suspicious bugs found! \ N ") ;}// if (k! = T) {// This method returns PE. Please note that printf ("\ n"); //} return 0 ;}