Codefources 519E. A and B and Lecture Rooms LCA

本文介绍了一种算法,用于解决在给定树结构中寻找同时等距于两个指定节点的所有可能节点数量的问题。该算法首先进行预处理,记录每个节点的孩子节点数量,并通过倍增法高效求解最近公共祖先(LCA),最终根据节点间距离和位置关系计算出等距节点的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



简单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;
}


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

资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 华为移动服务(Huawei Mobile Services,简称 HMS)是一个全面开放的移动服务生态系统,为企业和开发者提供了丰富的工具和 API,助力他们构建、运营和推广应用。其中,HMS Scankit 是华为推出的一款扫描服务 SDK,支持快速集成到安卓应用中,能够提供高效且稳定的二维码和条形码扫描功能,适用于商品扫码、支付验证、信息获取等多种场景。 集成 HMS Scankit SDK 主要包括以下步骤:首先,在项目的 build.gradle 文件中添加 HMS Core 库和 Scankit 依赖;其次,在 AndroidManifest.xml 文件中添加相机访问和互联网访问权限;然后,在应用程序的 onCreate 方法中调用 HmsClient 进行初始化;接着,可以选择自定义扫描界面或使用 Scankit 提供的默认扫描界面;最后,实现 ScanCallback 接口以处理扫描成功和失败的回调。 HMS Scankit 内部集成了开源的 Zxing(Zebra Crossing)库,这是一个功能强大的条码和二维码处理库,提供了解码、生成、解析等多种功能,既可以单独使用,也可以与其他扫描框架结合使用。在 HMS Scankit 中,Zxing 经过优化,以更好地适应华为设备,从而提升扫描性能。 通常,ScanKitDemoGuide 包含了集成 HMS Scankit 的示例代码,涵盖扫描界面的布局、扫描操作的启动和停止以及扫描结果的处理等内容。开发者可以参考这些代码,快速掌握在自己的应用中实现扫码功能的方法。例如,启动扫描的方法如下: 处理扫描结果的回调如下: HMS Scankit 支持所有安卓手机,但在华为设备上能够提供最佳性能和体验,因为它针对华为硬件进行了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值