Study and check the set,
If the root node of the 2 numbers is the same, it means they are in the same collection, detecting whether they are the opposite sex, if not the bug
If they are not the same, then determine if they are the opposite sex, if not, turn the gender of one of the sets upside down, merge them, or merge directly
#include <iostream> #define MAXN 2000+5using namespace Std;int n,m,flag;int f[maxn];int sign[maxn];void Ready () { for (int i=1;i<maxn;i++) f[i]=i,sign[i]=0;flag=1;} int dfs (int x) {if (f[x]!=x) F[x]=dfs (f[x]); return f[x];} void change (int x) {for (int i=1;i<=n;i++) {if (Dfs (x) ==dfs (i)) sign[i]=sign[i]^1;}} void build (int x,int y) {if (Dfs (x)!=dfs (y)) {if (Sign[x]==sign[y]) change (y); F[dfs (x)]=dfs (y);} Else{if (Sign[x]==sign[y]) flag=0;} int main () {Cin.sync_with_stdio (false); int T,casee=1,x,y;cin>>t;while (t--) {Ready ();cin>>n>>m; while (m--) {cin>>x>>y;build (x, y);} cout<< "Scenario #" <<casee++<< ":" <<endl;if (flag) cout<< "No suspicious bugs found!"; else cout<< "suspicious bugs found!"; Cout<<endl<<endl;} return 0;}
HDU 1829 A Bug ' s life