This is somewhat more advanced than basic query sets.
The following link provides a detailed explanation:
Http://blog.csdn.net/ditian1027/article/details/20804911
The relationship between every two animals is the relationship between push first and the final, and between push backward and the other;
Num stores the relationship with the final node;
# Include <stdio. h> # include <string. h ># include <iostream> using namespace STD; const int maxn = 50000 + 10; struct node {int Q, num;} s [maxn]; void QQ (int n) {for (INT I = 1; I <= N; I ++) {s [I]. Q = I; s [I]. num = 0 ;}} int find (INT y) {If (y = s [Y]. q) return y; int T = s [Y]. q; s [Y]. Q = find (s [Y]. q); s [Y]. num = (s [T]. num + s [Y]. num) % 3; return s [Y]. q;} void show (int q, int W, int e) // merge the set {int TT = find (Q); int RR = find (w ); s [RR]. Q = tt; s [RR]. Num = (s [Q]. num-s [w]. num + 3 + (E-1) % 3;} int main () {int A, B, n, m, G; scanf ("% d", &, & B); QQ (a); int ans = 0; while (B --) {scanf ("% d", & N, & M, & G); If (M> A | G> A) {ans ++; continue;} If (n = 2 & M = g) {ans ++; continue;} If (find (m) = find (G) {If (n = 1 & S [M]. num! = S [g]. Num) ans ++; If (n = 2 & (s [M]. Num + 1) % 3! = S [g]. Num) ans ++;} else show (M, G, n);} printf ("% d \ n", ANS); Return 0 ;}