I thought it was quite advanced and looked at the set, it was a flood problem,
N Cities n Balls, M queries, t means the ball of the same city as the X number is transferred to the city where Y Ball is located, Q indicates the city in which the X ball is asked, how many balls the city has, and the X-ball has been transferred several times.
1#include <iostream>2#include <cstdio>3#include <fstream>4#include <algorithm>5#include <cmath>6#include <deque>7#include <vector>8#include <queue>9#include <string>Ten#include <cstring> One#include <map> A#include <stack> -#include <Set> - #defineLL Long Long the #defineEPS 1e-8 - #defineINF 0x3f3f3f3f - #defineOpen_file - #defineMAXN 10005 + using namespacestd; - intN, M; + intFATHER[MAXN], STEP[MAXN], NUM[MAXN]; A //Step[x] Indicates how many times x has moved, Num[x] indicates that there are several balls in the X city at intFindintx) { - if(Father[x] = = x)returnx; - inty =Father[x]; -FATHER[X] =find (Father[x]); -STEP[X] + =Step[y]; - returnFather[x]; in } - to intMain () + { - #ifdef Open_file theFreopen ("In.txt","R", stdin); * //freopen ("OUT.txt", "w", stdout); $ #endif //Open_filePanax Notoginseng intT; -scanf"%d", &T); the for(intCAS =1; CAS <= T; cas++){ +scanf"%d%d", &n, &m); A for(inti =1; I <= N; i++){ theFather[i] =i; +Step[i] =0; -Num[i] =1; $ } $printf"Case %d:\n", CAs); - Charch; - intx, y; the for(inti =1; I <= m; i++){ -scanf"\ n");Wuyiscanf"%c", &ch); the if(ch = ='T'){ -scanf"%d%d", &x, &y); Wu //move all the balls in the same city as X to the city where Y is located - intA = find (x), B =find (y); AboutNUM[B] + =Num[a]; $Num[a] =0; -step[a]++; -Father[a] =b; - } A if(ch = ='Q'){ +scanf"%d", &x); the intA =find (x); -printf"%d%d%d\n", A, num[a], step[x]); $ } the } the } the}
HDU 3635 Dragon Balls with right and check set