HDU 1829 A Bug's Life (query set), hdu1829
Question link: click here.
Question and idea: Given the relationship between n Bugs (shen me gui) and m-to-Bugs, assuming the relationship is the relationship between men and women of the two Bugs, the question storage does not exist.Homosexuality.
So if a and B are men and women, and B and c are men and women, then we can think that the gender of a and c is the same. We can establish two sets and check the relationship between male and female.
If there is a ring in the male relationship (that is, there is a common root), then there will be a gay relationship.
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # define N 2000 + 10 using namespace std; int OK; int f [N], p [N]; // p [] indicates the relationship between men and women. Void Init (int n) {for (int I = 1; I <= n; I ++) {f [I] = I; p [I] = 0 ;}} int Find (int x) {return x = f [x]? X: f [x] = Find (f [x]);} void Merge (int a, int B) // a has a male relationship with B. {Int ra = Find (a), rb = Find (B); if (ra = rb) {// gay. OK = 0; return;} if (p [ra]) f [p [ra] = rb; // p [ra] and rb are in the same relationship (male) if (p [rb]) f [p [rb] = ra; // p [rb] And ra are in the same relationship (female) p [ra] = rb; p [rb] = ra;} int main () {int T, C = 0; scanf ("% d", & T); while (T --) {int n, m; scanf ("% d", & n, & m); Init (n); OK = 1; for (int I = 0; I <m; I ++) {int a, B; scanf ("% d", & a, & B); Merge (a, B );} printf ("Scenario # % d: \ n", ++ C); if (OK) puts ("No suspicious bugs found! "); Else puts (" Suspicious bugs found! "); Puts (" ");} return 0 ;}