SP14932 LCA - Lowest Common Ancestor-LCA

本文深入解析了在给定树结构中寻找两个节点的最近公共祖先(LCA)的问题,通过具体示例和算法实现,详细介绍了如何构建和遍历树形结构以高效解决LCA问题。

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

SP14932 LCA - Lowest Common Ancestor

Description:

一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树。-Wikipedia

最近公共祖先(LCA)是……(此处省去对LCA的描述),你的任务是对一棵给定的树TT以及上面的两个节点u,vu,v求出他们的LCALCA。

例如图中99和1212号节点的LCALCA为33号节点

Input:

输入的第一行为数据组数TT,对于每组数据,第一行为一个整数N(1\leq N\leq1000)N(1≤N≤1000),节点编号从11到NN,接下来的NN行里每一行开头有一个数字M(0\leq M\leq999)M(0≤M≤999),MM为第ii个节点的子节点数量,接下来有MM个数表示第ii个节点的子节点编号。下面一行会有一个整数Q(1\leq Q\leq1000)Q(1≤Q≤1000),接下来的QQ行每行有两个数u,vu,v,输出节点u,vu,v在给定树中的LCALCA。

输入数据保证只有一个根节点并且没有环。

Output:

对于每一组数据输出Q+1Q+1行,第一行格式为"Case i:"(没有双引号),i表示当前数据是第几组,接下来的QQ行每一行一个整数表示一对节点u,vu,v的LCALCA。

Sample Input:

1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7

Sample Output:

Case 1:
3
1

Translated by @yxl_gl

题目描述

A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia

The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia

Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.

For example the LCA of nodes 9 and 12 in this tree is the node number 3.

Input

The first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 <= N <= 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 <= M <= 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 <= Q <= 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 <= v, w <= 1,000.

Input will guarantee that there is only one root and no cycles.

Output

For each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.

Example

Input:
1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7

Output:
Case 1:
3
1

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+6;
bool vis[maxn];
int pre[maxn];
struct node{
	int to;
	int from;
	int LCA;
	int next;
}sz[maxn],sr[maxn];
int numadd1,numadd2;
int q=1;
int head[maxn],ahead[maxn];
int find(int x)
{
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
void add(int from ,int to)
{
	sz[++numadd1].next=head[from];
	sz[numadd1].to=to;
	head[from]=numadd1;
}
void add1(int from,int to)
{
	sr[++numadd2].next=ahead[from];
	sr[numadd2].to=to;
	ahead[from]=numadd2;
}
void dfs(int x)
{
//	cout<<x<<endl;
	pre[x]=x;
	vis[x]=1;
	for(int i=head[x];i;i=sz[i].next)
	{
		if(vis[sz[i].to]==0)
		{
			dfs(sz[i].to);
			pre[sz[i].to]=x;
		}
	}
	for(int i=ahead[x];i;i=sr[i].next)
	{
		if(vis[sr[i].to])
		{
			sr[i].LCA=find(sr[i].to);
			if(i&1)	sr[i+1].LCA=sr[i].LCA;
			else sr[i-1].LCA=sr[i].LCA;
		}
	}
}
void init()
{
	memset(vis,0,sizeof vis);
//	memset(sz,0,sizeof sz);
//	memset(sr,0,sizeof sr);
	memset(head,0,sizeof head);
	memset(ahead,0,sizeof ahead);
	numadd1=0,numadd2=0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{	
		init();//初始化 
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)//遍历给出边的联系 
		{
			int x;
			scanf("%d",&x);
			if(x==0)	continue;
			else
			{
				for(int j=0;j<x;j++)
				{
					int y;
					scanf("%d",&y);
					add(i,y);
					add(y,i);	
				}	
			} 
		}
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			add1(x,y);
			add1(y,x);
		}
		dfs(1);
		printf("Case %d:\n",q++);
		for(int i=1;i<=m;i++)
		{
			printf("%d\n",sr[i*2].LCA);
		}
	}
}

 

资源下载链接为: 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、付费专栏及课程。

余额充值