Tree links are separated by water, and can be modified at a single point. You can use a tree array.
# Include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <algorithm> # define n 250100 using namespace STD; int n, m, nowplace = 0; int P [N] = {0}, next [N], V [N], bnum = 0; int son [N] = {0 }, fa [N], W [N], top [N], deep [N] = {0}, num [N]; int T [N] = {0 }; int lowbit (int x) {return X &-X;} void dfs_1 (INT now, int fat) {int K = P [now]; num [now] = 1; int Maxson = 0; Fa [now] = fat; deep [No W] = deep [Fat] + 1; while (k) {dfs_1 (V [K], now); If (Num [V [k]> Maxson) {Maxson = num [V [k]; son [now] = V [k];} num [now] + = num [V [k]; k = next [k] ;}} void dfs_2 (INT now, int nowtop) {int K = P [now]; W [now] =++ nowplace; top [now] = nowtop; If (son [now]) dfs_2 (son [now], nowtop); While (k) {If (V [k]! = Son [now]) dfs_2 (V [K], V [k]); k = next [k] ;}} void add (INT now, int addnum) {While (now <= N) {T [now] + = addnum; now + = lowbit (now);} return;} int task (INT now) {int ans = 0; while (now) {ans + = T [now]; now-= lowbit (now);} return ans;} int ask (int u, int v) {int F1 = top [u], f2 = top [v]; If (deep [F1] <deep [F2]) {swap (F1, F2 ); swap (u, v);} If (F1 = F2) Return Task (max (W [u], W [v]) -task (min (W [u], W [v])-1); else {int ans = 0; ans = task (W [u]) -task (W [F1]-1); // find out the order of ANS + = Ask (Fa [F1], V); Return ans ;}} int main () {scanf ("% d", & N); For (INT I = 1; I <n; ++ I) {int X, Y; scanf ("% d", & X, & Y); If (x> Y) Swap (x, y); bnum ++; next [bnum] = P [X]; P [x] = bnum; V [bnum] = y;} dfs_1 (1, 0); dfs_2 (1, 1 ); for (INT I = 2; I <= N; ++ I) add (W [I], 1); scanf ("% d", & M ); M = m + n-1; For (INT I = 1; I <= m; ++ I) {char s [2]; scanf ("% s", S ); if (s [0] = 'A') {int X, Y; scanf ("% d", & X, & Y); If (x> Y) swap (x, y); add (W [Y],-1) ;}else {int X; scanf ("% d", & X ); printf ("% d \ n", ask (1, x) ;}} return 0 ;}
1103 poi2007 metropolitan Meg