This question is easy to understand: Enter the number of samples T, and then input the number N and M of bugs in each group of data. The bug number is 1 -- n! Then, enter the numbers a and B of the M group's cross-sex bugs, and then ask us to determine whether there is a set of same-sex bugs in this group of data, which is in conflict with the given data, then the corresponding result is output. If no result exists, another result is output!
Question: use this question and check the set. Each time before processing A and BYou need to determine whether A and B have been mated with other heterosexual bugs! Then, after processing a and B, judge whether a and B belong to the same branch of the tree. If yes, it indicates that a and B are same-sex bugs!
The Code is as follows:
# Include <cstdio> # include <cstring> # include <iostream> using namespace STD; int yix [2014], F [2014]; void Init (int n) {for (INT I = 1; I <= N; I ++) {f [I] = I; yix [I] = 0 ;}} int find (INT X) {return f [x] = x? X: Find (F [x]);} void Unity (int A, int B) {// merge the same-sex bug int x = find (); int y = find (B); If (X! = Y) f [x] = y;} int main () {int t; CIN> T; For (INT I = 1; I <= T; I ++) {bool tx_istrue = false; int n, m, a, B; CIN> N> m; Init (n); While (M --) {// If yix [num] is not 0, it indicates that num must have been mated with the opposite sex bug numbered yix [num! Cin> A> B; If (! Yix [a] &! Yix [B]) {yix [a] = B; // if neither of the two Bugs has been detected, yix [B] =; // mark the array as the opposite sex!} Else if (! Yix [a] & yix [B]) {yix [a] = B; unity (A, yix [B]); // The bug number A must be the same as the bug number yix [B!} Else if (yix [a] &! Yix [B]) {yix [B] = A; unity (B, yix [a]); // The bug number B must be the same as the bug number yix [!} Else {// here is the judgment I mentioned above. It indicates that both A and B have mated with the opposite sex bug numbered yix [a] and yix [B] before that! Unity (yix [a], B); unity (yix [B], a);} If (find (A) = find (B) tx_istrue = true; // each input is used to determine whether a and B are the same type!} Printf ("Scenario # % d: \ n", I); If (tx_istrue) printf ("suspicious bugs found! \ N "); else printf (" no suspicious bugs found! \ N "); printf (" \ n ");} return 0 ;}
Poj-2492-A Bug's Life