Simple LCA:
The number of points on the tree that are equal from the given two points
the number of children per node is preprocessed first sum[x], To find out the LCA of a, B, we can know the distance between two points according to the depth, the distance is the solution of even .
Depending on the location of the LCA between a A and a, different points of discussion:
Set A and LCA distance is ha, b and LCA distance is HB
1:lca is in the middle of a, a, A and b respectively belong to the two subtree of the LCA, the result is: n-sum[a upward distance LCA ha-1 Point]-sum[b upward distance LCA hb-1 Point]
2:a,b two points relative to LCA one on the other. C: The result of the bottom point of a A, B is: sum[lca]-sum[C upward distance from LCA Hc-1]
Evaluation of LCA O (Logn) by multiplication method, total time complexity O (MLOGN)
E. A and B and lecture roomstime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputoutputstandar D Output
< Span class= "tex-font-style-it" style= "Font-style:italic" >a and B is preparing themselves for programming contests.
The University where A and B study are a set of rooms connected by corridors. Overall, the University has n ?-? 1 corridors So, ' can get ' from any ' and ' on ' E by moving along the corridors. The rooms is numbered From 1 to
Every Dayаand B write contests in some rooms of their university, and after each contest they gather together in the SAM e-discuss problems. A and B want the distance from the rooms where problems is discussed to the rooms where contests is written to be equal. The distance between and rooms are the number of edges on the shortest path between them.
As they write contests in new rooms every day, they asked "help them" find the number of possible rooms to discuss PR Oblems for each of the following m days.
Input
The first line contains integer n (1?≤? N? ≤?105)-the number of rooms in the University.
The next n?-? 1Lines describe the corridors. TheI-th of these lines (1?≤? i? ≤? n?-? 1) contains-integers ai and bi (1?≤? a i,? b i? ≤? N ), showing that theI-th Corridor connects Rooms ai and bi .
The next line contains integer m (1?≤? M.≤?105)-the number of queries.
NextmLines describe the queries. TheJ-th of these lines (1?≤? j. ≤? m ) contains-integers xJ and yJ (1?≤? x J,? y j. ≤? N ) That means theJ-th Day A would write the contest in the xJ , B'll write in the yJ .
Output
In the i-th (1?≤? I? ≤? m) line print the number of rooms that is equidistant from the rooms where A and B write contest on the i-th Day.
Sample Test (s) input
41 21 32) 412 3
Output
1
Input
41 22 32 421 21 3
Output
02
Note
In the first sample there are only one for the same distance from rooms number 2 and 3-room number 1.
/* ***********************************************author:ckbosscreated time:2015 March 05 Thursday 16:52 05 seconds file Name : e.cpp************************************************ * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib># Include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int MAXN =100100;const int deg=20;struct edge{int to,next;} Edge[maxn*2];int adj[maxn],size;void Init_edge () {memset (adj,-1,sizeof (ADJ)); size=0;} void Add_edge (int u,int v) {Edge[size].to=v;edge[size].next=adj[u]; adj[u]=size++;} int fa[maxn][deg];int deg[maxn];void BFS (int root) {queue<int> Q;deg[root]=0;fa[root][0]=root;q.push (root); while (!q.empty ()) {int U=q.front (), Q.pop (), for (int i=1;i<deg;i++) fa[u][i]=fa[fa[u][i-1]][i-1];for (int i=adj[u]; ~i;i=edge[i].next) {int v=edge[i].to;if (v==fa[u][0]) continue;deg[v]=deg[u]+1;fa[v][0]=u;q.Push (v);}}} int LCA (int u,int v) {if (Deg[u]>deg[v]) swap (U,V); int hu=deg[u],hv=deg[v];int tv=v,tu=u;for (int det=hv-hu,i=0;det;i ++,DET=DET/2) if (det&1) tv=fa[tv][i];if (TV==TU) return tu;for (int i=deg-1;i>=0;i--) {if (Fa[tu][i]==fa[tv][i]) Continue;tu=fa[tu][i]; Tv=fa[tv][i];} return fa[tu][0];} int n,m;int sum[maxn];int DFS (int u) {int ret=1;for (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to;if (v==fa[u][0]) Continue;ret+=dfs (v);} return Sum[u]=ret;} void solve (int x,int y) {if (x==y) {printf ("%d\n", n); return;} int Lca=lca (x, y), int h1=deg[x]-deg[lca];int h2=deg[y]-deg[lca];if ((H1+H2)%2==1) {puts ("0"); return;} if (H1==H2) {int p1=x,p2=y;for (int det= (H1+H2)/2-1,i=0;det;i++,det/=2) if (det&1) p1=fa[p1][i];for (int det= (H1+H2) /2-1,i=0;det;i++,det/=2) if (det&1) p2=fa[p2][i];p rintf ("%d\n", N-sum[p1]-sum[p2]);} Else{if (Deg[x]<deg[y]) swap (x, y), int p1=x,p2=x;for (int det= (H1+H2)/2,i=0;det;i++,det/=2) if (det&1) P1=FA[P1] [I];for (int det= (H1+H2)/2-1,i=0;det;i++,det/=2) if (det&1) p2=fa[P2][i];int ANS=SUM[P1]-SUM[P2];p rintf ("%d\n", ans);}} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); Init_edge (); scanf ("%d", &n), for (int i=0;i<n-1;i++) {int a,b;scanf ("%d%d", &A,&B); Add_edge (A, b); Add_edge (b,a);} BFS (1); DFS (1); scanf ("%d", &m), while (m--) {int a,b;scanf ("%d%d", &a,&b); Solve (A, b);} return 0;}
Codefources 519E. A and B and lecture Rooms LCA