A and B and Lecture Rooms(LCA)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A and B and Lecture Rooms(LCA)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/input' title='input'>input
line follow OWIN -- test oom += rri棰樼洰鎻忚堪
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?-?1 corridors 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.
Examples
4
1 2
1 3
2 4
1
2 3
1
4
1 2
2 3
2 4
2
1 2
1 3
0
2
瑙f瀽锛?/span>
缁欏嚭鏍戜笂涓ょ偣u,v锛屾壘鍒颁笌u,v璺濈鐩哥瓑鐨勭偣鐨勪釜鏁?br>鍏堟壘鍒皍,v鐨勫叕鍏辩鍏坮,鐒跺悗鐭ラ亾u,v闂寸殑璺濈锛屼腑闂翠綅缃负mid锛?br>褰撶劧鑻is(u,v)涓哄鏁板垯鏃犺В锛?br>灏唌id鐨勪綅缃繘琛岃璁猴細
mid鍦╮涓婏細
mid涓嶅湪r涓婏細
//姣掔槫棰? #include<cstring> #include<cstdio> #include<algorithm> #include<iostream> #include<vector> #include<queue> using namespace std; vector<int>G[140000]; int n,m; int size[140000],grand[140000][21],deep[140000]; //鏁版嵁鑼冨洿銆佹暟缁勮寖鍥磋娉ㄦ剰 void build(int x,int pre) { for(int i=1;i<=20;i++) { grand[x][i]=grand[grand[x][i-1]][i-1]; } size[x]=1; for(int i=0;i<G[x].size();i++) { int v=G[x][i]; if(v==pre)continue; deep[v]=deep[x]+1; grand[v][0]=x; build(v,x); size[x]+=size[v]; } } int lca(int u,int v) { if(deep[u]<deep[v])swap(u,v); int dis=deep[u]-deep[v]; /////////////////////////////////// for(int k=0;k<19;k++){ if((dis>>k)&1){ u=grand[u][k]; } } //鎶妘璋冨埌鍜寁鍚屼竴娣卞害 /////////////////////////////////// if(u==v)return u;//褰撲笖鍙綋u銆乿澶勪簬涓€鏉¢摼鏃? for(int k=19;k>=0;k--){ if(grand[u][k]!=grand[v][k]){ u=grand[u][k]; v=grand[v][k]; } } return grand[u][0]; } int main() { scanf("%d",&n); for(int i=1;i<n;i++) { int u,v;scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u);//vector鍙互...鑺傜害浠g爜閲? } build(1,0);//寤烘爲 scanf("%d",&m);//m涓闂? for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); int r=lca(u,v); int dis=deep[u]+deep[v]-2*deep[r]; if(dis&1){printf("0 ");} if(deep[u]>deep[v])swap(u,v); else { dis/=2; ///////////////////////////////////////////////// int mid=v; for(int k=19;k>=0;k--){ if((dis>>k)&1) mid=grand[mid][k]; } /////////////////////////鎵惧埌 mid int ans=1; if(mid==r) { int preu=u,prev=v; int du=deep[u]-deep[r];du--; int dv=deep[v]-deep[r];dv--; for(int k=19;k>=0;k--){ if((du>>k)&1) preu=grand[preu][k]; if((dv>>k)&1) prev=grand[prev][k]; } ans=n-size[preu]-size[prev]; } else { int prev=v,preu=u; int dv=deep[v]-deep[mid]; dv--; for(int k=19;k>=0;k--){ if((dv>>k)&1) prev=grand[prev][k]; } ans=size[mid]-size[prev]; } cout<<ans<<endl; } } }
以上是关于A and B and Lecture Rooms(LCA)的主要内容,如果未能解决你的问题,请参考以下文章
[Codeforces#519E] A and B and Lecture Rooms
Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
Henu ACM Round#15 E A and B and Lecture Rooms