This feeling and POJ 1988 Cube stacking very much like, when the path is compressed at the same time recursion out with the new distant array
1 //#define LOCAL2#include <algorithm>3#include <cstdio>4 using namespacestd;5 6 Const intMAXN =20000+Ten;7 intPARENT[MAXN], DISTANT[MAXN];8 9 intGetParent (inta)Ten { One if(Parent[a] = = a)returnA; A intRoot =GetParent (Parent[a]); -Distant[a] + =Distant[parent[a]]; - returnParent[a] =Root; the } - - intMainvoid) - { + #ifdef LOCAL -Freopen ("3027in.txt","R", stdin); + #endif A at intT, N; -scanf"%d", &T); - while(t--) - { -scanf"%d", &n); - for(inti =1; I <= N; ++i) in { -Parent[i] =i; toDistant[i] =0; + } - GetChar (); the Charp; * while(SCANF ("%c", &p) = =1&& P! ='0') $ {Panax Notoginseng intA, B; - if(p = ='E') the { +scanf"%d", &a); A GetParent (a); theprintf"%d\n", Distant[a]); + } - Else $ { $scanf"%d%d", &a, &b); -Parent[a] =b; -Distant[a] = ABS (A-B)% +; the } - GetChar ();Wuyi } the } - return 0; Wu}
code June
LA 3027 corporative Network