Online block tree LCA template.
1 # include <cstdio> 2 # include <vector> 3 # include <algorithm> 4 # include <cmath> 5 using namespace std; 6 # define N 30001 7 vector <int> G [N]; 8 typedef vector <int>: iterator ITER; 9 int dep [N], x, y, fa [N], top [N], siz [N], sz, n, m, ans; 10 int Abs (const int & x) {return x <0? (-X): x;} 11 void makeblock (int U) 12 {13 for (ITER it = G [U]. begin (); it! = G [U]. end (); it ++) 14 if (* it )! = Fa [U]) 15 {16 fa [* it] = U; 17 dep [* it] = dep [U] + 1; 18 if (siz [top [U] <sz) 19 {20 top [* it] = top [U]; 21 siz [top [U] ++; 22} 23 makeblock (* it); 24} 25} 26 int lca (int U, int V) 27 {28 while (U! = V) 29 {30 if (top [U]! = Top [V]) 31 {32 if (dep [top [U] <dep [top [V]) swap (U, V ); 33 U = fa [top [U]; 34} 35 else36 {37 if (dep [U] <dep [V]) swap (U, V ); 38 U = fa [U]; 39} 40} 41 return U; 42} 43 int main () 44 {45 scanf ("% d", & n ); 46 for (int I = 1; I <n; I ++) 47 {48 scanf ("% d", & x, & y ); 49G [x]. push_back (y); 50G [y]. push_back (x); 51} sz = (int) sqrt (double) n); 52 for (int I = 1; I <= n; I ++) siz [I] = 1, top [I] = I; 53 makeblock (1); scanf ("% d", & m, & x ); 54 for (int I = 2; I <= m; I ++) 55 {56 scanf ("% d", & y); int f = lca (x, y); 57 ans + = (dep [x] + dep [y]-(dep [f] <1); 58 x = y; 59} 60 printf ("% d \ n", ans); 61 return 0; 62}
[Recent common ancestor] [block tree] CODEVS 1036 business trip