Every time there is a new understanding.
Think there is no heap at the bottom for ancestors.
About the Findset is how to maintain, this time and a good understanding, the problem is each update x and X of the last layer of the node, from the bottom up (from the ancestor layer to itself).
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <cmath> -#include <stdlib.h> -#include <vector> the#include <stack> -#include <Set> - #defineINF 1e7 - #defineMAXN 10010 + #defineMAXN 1000010 - #defineMod 1000007 + #defineN 1010 A using namespacestd; attypedefLong LongLL; - - intN, M; - intfa[333333]; - intnum[333333]; - intun[333333]; in - //take the bottom of every pile as ancestors to //update from ancestor to layer down one level + intFindset (intx) - { the if(x = = Fa[x])returnx; * intTMP =Findset (fa[x]); $UN[X] + =Un[fa[x]];Panax NotoginsengFA[X] =tmp; - returnFa[x]; the } + A voidMerg (intAintb) the { + intx =Findset (a); - inty =Findset (b); $ if(X! =y) { $UN[X] =Num[y]; -Num[y] + =Num[x]; -FA[X] =y; the } - }Wuyi the intMain () - { Wu intA, B; - Charch; About while(~SCANF ("%d",&N)) { $ GetChar (); - for(inti =0; I <= N; ++i) -Fa[i] = i, num[i] =1, un[i] =0; - A for(inti =0; I < n; ++i) { +scanf"%c",&ch); the if(ch = ='M') { -scanf"%d%d",&a,&b); $ GetChar (); the Merg (A, b); the } the Else { thescanf"%d",&a); - GetChar (); in Findset (a); theprintf"%d\n", Un[a]); the } About } the } the return 0; the}
HDU 2818 Building Block (and check set)