codeforces 519E A and B and Lecture Rooms(LCA,倍增)

时间:2022-02-15 20:53:09

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

E. A and B and Lecture Rooms

A and B are preparing themselves for programming contests.

The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.

Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.

As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems 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 - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.

The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.

Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.

Output

In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.

Sample test(s)
input
4
1 2
1 3
2 4
1
2 3
output
1
input
4
1 2
2 3
2 4
2
1 2
1 3
output
0
2
Note

in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.

 

题意:

给出一棵树,有m次查询,每次查询给出两个点,要求求出这棵树上所有到这两个点距离相等的点的数目

分析:

首先求出两个点的距离,若距离为奇数,则不存在满足要求的点,若距离为偶数,则去路径上的中点,求出所有以从中点连出的分支,同时减去与给出的两点在同一分支上的点的数目

考虑到后面要求深度较深的点向上爬距离为两点间距离一半路程,用倍增在前面处理过的话,这里可以用log n 得到,所以采取了倍增

  1 //#####################
  2 //Author:fraud
  3 //Blog: http://www.cnblogs.com/fraud/
  4 //#####################
  5 #include <iostream>
  6 #include <sstream>
  7 #include <ios>
  8 #include <iomanip>
  9 #include <functional>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <string>
 13 #include <list>
 14 #include <queue>
 15 #include <deque>
 16 #include <stack>
 17 #include <set>
 18 #include <map>
 19 #include <cstdio>
 20 #include <cstdlib>
 21 #include <cmath>
 22 #include <cstring>
 23 #include <climits>
 24 #include <cctype>
 25 using namespace std;
 26 #define XINF INT_MAX
 27 #define INF 0x3FFFFFFF
 28 #define MP(X,Y) make_pair(X,Y)
 29 #define PB(X) push_back(X)
 30 #define REP(X,N) for(int X=0;X<N;X++)
 31 #define REP2(X,L,R) for(int X=L;X<=R;X++)
 32 #define DEP(X,R,L) for(int X=R;X>=L;X--)
 33 #define CLR(A,X) memset(A,X,sizeof(A))
 34 #define IT iterator
 35 typedef long long ll;
 36 typedef pair<int,int> PII;
 37 typedef vector<PII> VII;
 38 typedef vector<int> VI;
 39 #define MAXN 100010
 40 vector<int>G[MAXN];
 41 int root;
 42 int size[MAXN];
 43 int tot;
 44 int pa[20][MAXN];
 45 int depth[MAXN];
 46 void dfs(int v,int fa,int d){
 47     pa[0][v]=fa;
 48     size[v]=1;
 49     depth[v]=d;
 50     for(int i=0;i<G[v].size();i++){
 51         if(G[v][i]==fa)continue;
 52         dfs(G[v][i],v,d+1);
 53         size[v]+=size[G[v][i]];
 54     }
 55 }
 56 void init(){
 57     dfs(0,-1,0);
 58     for(int k=0;k+1<19;k++){
 59         for(int v=0;v<tot;v++){
 60             if(pa[k][v]<0)pa[k+1][v]=-1;
 61             else pa[k+1][v]=pa[k][pa[k][v]];
 62         }
 63     }
 64 }
 65 int lca(int u,int v){
 66     if(depth[u]>depth[v])swap(u,v);
 67     int dis=depth[v]-depth[u];
 68     for(int k=0;k<19;k++){
 69         if((dis>>k)&1){    
 70             v=pa[k][v];
 71         }
 72     }
 73     if(u==v)return u;
 74     for(int k=19;k>=0;k--){
 75         if(pa[k][u]!=pa[k][v]){
 76             u=pa[k][u];
 77             v=pa[k][v];
 78         }
 79     }
 80     return pa[0][u];
 81 }
 82 int main()
 83 {
 84     ios::sync_with_stdio(false);
 85     int n;
 86     cin>>n;
 87     tot=n;
 88     int u,v;
 89     for(int i=0;i<n-1;i++){
 90         cin>>u>>v;
 91         u--;v--;
 92         G[u].push_back(v);
 93         G[v].push_back(u);
 94     }
 95     init();
 96     int m;
 97     cin>>m;
 98     for(int i=0;i<m;i++){
 99         cin>>u>>v;
100         u--;v--;
101         int r=lca(u,v);
102         int dis=depth[u]+depth[v]-2*depth[r];
103         if(depth[u]>depth[v])swap(u,v);
104         if(dis&1){
105             cout<<0<<endl;
106         }else{
107             dis/=2;
108             int mid=v;
109             for(int k=19;k>=0;k--){
110                 if((dis>>k)&1){
111                     mid=pa[k][mid];
112                 }
113             }
114             int ans=1;
115             if(mid==r){
116                 int preu=u,prev=v;
117                 int du=depth[u]-depth[r];
118                 du--;
119                 for(int k=19;k>=0;k--){
120                     if((du>>k)&1){
121                         preu=pa[k][preu];
122                     }
123                 }
124                 int dv=depth[v]-depth[r];
125                 dv--;
126                 for(int k=19;k>=0;k--){
127                     if((dv>>k)&1){
128                         prev=pa[k][prev];
129                     }
130                 }
131                 ans=tot-size[preu]-size[prev];
132             }else{
133                 int prev=v,preu=u;
134                 int dv=depth[v]-depth[mid];
135                 dv--;
136                 for(int k=19;k>=0;k--){
137                     if((dv>>k)&1){
138                         prev=pa[k][prev];
139                     }
140                 }
141                 ans=size[mid]-size[prev];
142             }
143             cout<<ans<<endl;
144         }
145     }
146     
147     return 0;
148 }