Topic Link: please poke here.
Questions and ideas: given n Bugs (shen me gui) and m to bugs relationship, assuming that the relationship is two bugs of the relationship between men and women, then there is no homosexual situation.
So if A and b are male and female, B and C are male and female, then the gender of a and c we can think of as the same. We can build two and check sets, a class of male-male relations, a class of female relations.
So if there is a ring in the male-male relationship (that is, there is a common root), then the homosexual relationship appears.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define N 2000+10using namespace Std;int ok;int F[n],p[n]; Use p[] to express 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 and B have male and female relations. {int Ra=find (a), rb=find (b); if (RA==RB) {//presence of homosexuality. ok=0; Return } if (P[ra]) f[p[ra]]=rb; P[ra] and RB are the same relationship (male male) if (P[RB]) F[p[rb]]=ra; P[RB] and RA are 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%d", &n,&m); Init (n); ok=1; for (int i=0;i<m;i++) {int A, B; scanf ("%d%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;}
HDU 1829 A Bug ' s Life (and set)