And last night did a Bug ' s life is the same idea of the problem ... There are some small problems when you start doing it, just make progress on a variety of issues.
#include <cstdio> #define Size 100000int N, M;int rel[size+1], pre[size+1];void Init () {for (int i=1; i<=n; i++) {rel[i]=0;//oneself and own the same gang pre[i]=i; }}int getpre (int a) {if (pre[a] = = a) return A; int t = getpre (Pre[a]); Rel[a] = Rel[a]^rel[pre[a]]; Pre[a] = t; return pre[a];} void Union (int a, int b) {int Pa = Getpre (a); int Pb = Getpre (b); if (Pa = = Pb) return; PRE[PA] = Pb; REL[PA] = ~ (Rel[a]^rel[b]);} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n, &m); Init (); while (m--) {char C; int A, B; GetChar ();//How can this be forgotten ... scanf ("%c%d%d", &c, &a, &b); if (c== ' A ') { int Pa = Getpre (a); int Pb = Getpre (b); if (PA==PB) if (rel[a] = = Rel[b]) printf ("in t He same gang.\n "); else printf ("in different gangs.\n"); else printf ("Not sure yet.\n"); } else Union (A, b); }} return 0;}
"Go" poj-1703-find them, Catch them: The elements within the collection have a relationship, the relationship between the sets is indeterminate