Codefources 519E. A and B and Lecture Rooms LCA
来源:互联网 发布:php 数组第一个key 编辑:程序博客网 时间:2024/04/28 02:08
简单LCA:
求树上距离给定两个点a,b距离相等的点有多少个
先预处理出每个节点的孩子个数sum[x],求出a,b的LCA,根据深度就可以知道两个点的距离,距离为偶数的有解....
根据lca在a,b之间的位置不同分情况讨论:
设a与lca距离为 ha , b与lca距离为 hb
1:lca在a,b正中间既a,b分别属于lca的两个子树中, 结果为: n-sum[ a往上距离lca ha-1 的点] - sum[ b往上距离lca hb-1 的点]
2:a,b两个点相对lca一上一下. c:a,b中靠下的那个点 结果为: sum[lca]-sum[ c往上距离lca hc-1 的点 ]
用倍增法求LCA O(logn) , 总时间复杂度 O(mlogn)
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.
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 thej-th day A will write the contest in the room xj, B will write in the room yj.
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.
41 21 32 412 3
1
41 22 32 421 21 3
02
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
/* ***********************************************Author :CKbossCreated Time :2015年03月05日 星期四 16时52分05秒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];printf("%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];printf("%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
- Codefources 519E. A and B and Lecture Rooms LCA
- Codeforces 519E A and B and Lecture Rooms LCA
- codeforces 519E A and B and Lecture Rooms (lca)
- codeforces 519E E. A and B and Lecture Rooms(树形dp+lca)
- Codeforces 519E. A and B and Lecture Rooms (LCA)
- codeforces 519E A and B and Lecture Rooms
- CodeForces 519E A and B and Lecture Rooms
- 【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)
- 在线LCA 倍增法 Codeforces Round #294 (Div. 2) E - A and B and Lecture Rooms
- CF 294 div2 E. A and B and Lecture Rooms (LCA)
- Codeforces Round #294 (Div. 2) E. A and B and Lecture Rooms(倍增LCA+树形DP)
- Codeforces Round #294 (Div. 2)-E. A and B and Lecture Rooms(LCA倍增)
- [CodeForces519E]A and B and Lecture Rooms(lca)
- [codefoces519E]A and B and Lecture Rooms(lca)
- #294 (div.2) E.A and B and Lecture Rooms
- codeforces E. A and B and Lecture Rooms
- ARM内核全解析,从ARM7到Cortex A53,Cortex A57
- 程序员练级攻略----转自酷壳网coolshell.c n
- 玩转大数据之Apache Pig如何与Apache Lucene集成
- 浅析联发科研发Cortex-A53芯片背后的用意!
- 指针数组和数组指针的区别
- Codefources 519E. A and B and Lecture Rooms LCA
- HT For Web 拓扑图背景设置
- 吐槽:objective-c反人类
- 对于最长连续重复子串伤感经历
- Cocos2d-x 3.x学习笔记:猩先生带你打飞机(二)素材准备与游戏菜单场景
- ARM全新旗舰架构!Cortex-A72正式发布
- Cortex-A17内核发布:ARM彻底学坏了!
- 8核插上4G翅膀 MT6595借Cortex-A17领跑
- Cellular Automata相关matlab代码