国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > Codefources 519E. A and B and Lecture Rooms LCA

Codefources 519E. A and B and Lecture Rooms LCA

来源:程序员人生   发布时间:2015-03-14 09:51:45 阅读次数:3085次


简单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⑴ 的点] - sum[ b往上距离lca hb⑴ 的点] 

2:a,b两个点相对lca1上1下. c:a,b中靠下的那个点 结果为: sum[lca]-sum[ c往上距离lca hc⑴ 的点 ]


用倍增法求LCA  O(logn) , 总时间复杂度 O(mlogn) 



E. A and B and Lecture Rooms
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 thej-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.



/* *********************************************** Author :CKboss Created Time :2015年03月05日 星期4 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,⑴,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⑴]][i⑴]; 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⑴;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); 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⑴,i=0;det;i++,det/=2) if(det&1) p1=fa[p1][i]; for(int det=(h1+h2)/2⑴,i=0;det;i++,det/=2) if(det&1) p2=fa[p2][i]; printf("%d ",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⑴,i=0;det;i++,det/=2) if(det&1) p2=fa[p2][i]; int ans=sum[p1]-sum[p2]; printf("%d ",ans); } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); init_edge(); scanf("%d",&n); for(int i=0;i<n⑴;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; }



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生