Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)
2017-07-23 22:15
507 查看
[b]A and B and Lecture Rooms[/b]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
input
output
input
output
Note
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
【题意】给你一棵树,然后m次询问,每次询问给你两个点u,v,问有多少点到u和v的距离相等。
【分析】先找lca算距离,若距离是奇数,则输出0;若是偶数,两种情况,一,lca就是中点,二,不是...很简单,直接上代码...
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
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次询问,每次询问给你两个点u,v,问有多少点到u和v的距离相等。
【分析】先找lca算距离,若距离是奇数,则输出0;若是偶数,两种情况,一,lca就是中点,二,不是...很简单,直接上代码...
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define vi vector<int> #define pii pair<int,int> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) using namespace std; typedef long long LL; const int N = 1e5+50; const int mod = 1e9+7; int n,m; int dep ,fa [20],sz ; vector<int>edg ; void dfs(int u,int f){ fa[u][0]=f; sz[u]=1; for(int i=1;i<20;i++){ fa[u][i]=fa[fa[u][i-1]][i-1]; } for(int v : edg[u]){ if(v==f)continue; dep[v]=dep[u]+1; dfs(v,u); sz[u]+=sz[v]; } } int LCA(int u,int v){ int U=u,V=v; if(dep[u]<dep[v])swap(u,v); for(int i=19;i>=0;i--){ if(dep[fa[u][i]]>=dep[v]){ u=fa[u][i]; } } if(u==v)return (u); for(int i=19;i>=0;i--){ if(fa[u][i]!=fa[v][i]){ u=fa[u][i];v=fa[v][i]; } } return (fa[u][0]); } pii findMid(int u,int v,int lca){ if(dep[u]-dep[lca]>dep[v]-dep[lca])swap(u,v); int vv=v,mid; for(int i=19;i>=0;i--){ mid=fa[v][i]; int s1=dep[u]+dep[mid]-2*dep[lca]; int s2=dep[vv]-dep[mid]; if(dep[mid]<dep[lca]||s1<=s2)continue; else v=fa[v][i]; } return mp(v,fa[v][0]); } int main(){ int u,v; scanf("%d",&n); for(int i=1;i<n;i++){ scanf("%d%d",&u,&v); edg[u].pb(v); edg[v].pb(u); } dfs(1,0); scanf("%d",&m); while(m--){ scanf("%d%d",&u,&v); int lca=LCA(u,v); int s=dep[u]+dep[v]-2*dep[lca]; if(s&1)puts("0"); else if(u==v)printf("%d\n",n); else { if(dep[u]-dep[lca]==dep[v]-dep[lca]){ for(int i=19;i>=0;i--){ if(fa[u][i]!=fa[v][i]){ u=fa[u][i];v=fa[v][i]; } } printf("%d\n",n-sz[u]-sz[v]); } else { pii p=findMid(u,v,lca); int mid=p.second; int midson=p.first; printf("%d\n",sz[mid]-sz[midson]); } } } return 0; }
相关文章推荐
- Codeforces Round #294 (Div. 2)-E. A and B and Lecture Rooms(LCA倍增)
- 在线LCA 倍增法 Codeforces Round #294 (Div. 2) E - A and B and Lecture Rooms
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- CF 294 div2 E. A and B and Lecture Rooms (LCA)
- 【LCA】 Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms
- LCA好(Codeforces Round #294 (Div. 2)E. A and B and Lecture Rooms)
- [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(lca+思维,树上寻找与给定两个点距离相等的点的个数)
- Codeforces 519E A and B and Lecture Rooms [倍增法LCA]
- [codefoces519E]A and B and Lecture Rooms(lca)
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms
- Codeforces Round #294 (Div. 2)---E. A and B and Lecture Rooms
- Codeforces Round #294 (Div. 2)-E. A and B and Lecture Rooms
- [CodeForces519E]A and B and Lecture Rooms(lca)
- [codeforces 519E]E. A and B and Lecture Rooms(树上倍增)
- Codeforces Round #425 (Div. 2)D. Misha, Grisha and Underground(LCA)
- CF519E - A and B and Lecture Rooms
- Codeforces Round #343 (Div. 2) E. Famil Door and Roads lca 树形dp,lca
- CodeForces 519E 树形DP A and B and Lecture Rooms
- D. Misha, Grisha and Underground(LCA 倍增)