Codefources 519E. A and B and Lecture Rooms LCA

简单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)

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日 星期四 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;
}
时间: 2024-10-07 06:32:19

Codefources 519E. A and B and Lecture Rooms LCA的相关文章

codeforces 519E A and B and Lecture Rooms LCA倍增

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 519E Description A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected

Codeforces 519E A and B and Lecture Rooms LCA

题目链接:点击打开链接 题意: 给定n个点的树. 下面n-1行给出树 Q个询问. 每次询问 (u,v)问树上有多少个点到u点距离=到v点距离 思路: 首先这两个点的距离必须是偶数,若为奇数答案就是0 然后用lca找到中间节点即可. trick : u==v ans = n #include"cstdio" #include"iostream" #include"queue" #include"algorithm" #inclu

[Codeforces#519E] A and B and Lecture Rooms

Codeforces题号:#519E 出处: Codeforces 主要算法:最近公共祖先LCA(倍增法) 难度:4.5 思路分析: 题意:询问给出一棵无根树上任意两点a,b,求关于所有点i,\(dist(a,i) = dist(b,i)\)的点的数量.要求每一次询问在O(log n)的时间复杂度内完成. 由于在树上求距离,并且还要O(log n),自然会联想到LCA.由于边权是1,那么点到根的距离就是该点的深度.这个深度可以在dfs预处理的过程中处理完成.那么两个点之间的距离就是两个点到根节点

CodeForces 519E 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

Codeforces 519E A and B and Lecture Rooms [倍增法LCA]

题意: 给你一棵有n个节点的树,给你m次询问,查询给两个点,问树上有多少个点到这两个点的距离是相等的.树上所有边的边权是1. 思路: 很容易想到通过记录dep和找到lca来找到两个点之间的距离,然后分情况讨论. 一开始困扰我的问题是如果lca不是正中间的点,如何在比较低的复杂度的层面上求解中点. 倍增法lca不光可以在logn的时间复杂度内查询某两个点的lca,还可以实现在logm的时间复杂度能查询某个节点的第m个父亲节点. 算法的核心是用二进制的运算来实现查询. #include<bits/s

A and B and Lecture Rooms(LCA)

题目描述 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 oth

CodeForces 519E 树形DP A and B and Lecture Rooms

给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 100000 + 10; 9 10

#294 (div.2) E.A and B and Lecture Rooms

1.题目描述:点击打开链接 2.解题思路:本题让我长见识了.也学到了很多新的知识:LCA,多级祖先算法.如果只是单纯地将无根树转化为有根树,找到u,v的中点,再用BFS计算中线上结点的个数,那么最终会导致TLE.本题的高效算法如下: 首先求出以1为根的树的所有结点的总个数,保存在num数组中,再利用LCA算法求出u,v的公共祖先,设为LCA.找到u,v结点的中点mid.此时规定deep[u]>deep[v],不满足就交换两数.那么分两种情况讨论:(i)如果d(u,LCA)==d(v,LCA)(d

Codeforces Round #294 Div2 E(A and B and Lecture Rooms)

Problem 给一棵树,含有N个节点,N?1条边.进行M次查询,每次给定两个节点x,y,问树上有多少个节点到x,y的距离相同. Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,M∈[1,105] x,y∈[1,N] Look up Original Problem From here Solution 求出a,b两个节点的lca,再找到lca?>a或lca?>b上的某个节点v使得dis(v,a)=dis(v,b).分v=lca,v∈lca?&g