poj 1330 Nearest Common Ancestors

题目连接

http://poj.org/problem?id=1330  

Nearest Common Ancestors

Description

A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: 

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. 

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. 

Write a program that finds the nearest common ancestor of two distinct nodes in a tree. 

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.。

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5

Sample Output

4
3

LCA裸题,测模板。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 10010;
struct Tarjan_Lac {
	bool vis[N];
	struct edge { int to, next; }G[N << 1];
	int A, B, tot, ans, root, par[N], head[N];
	inline void init(int n) {
		ans = tot = 0;
		for (int i = 0; i < n + 2; i++) {
			par[i] = i;
			vis[i] = false;
			head[i] = -1;
		}
	}
	inline void add_edge(int u, int v) {
		G[tot].to = v, G[tot].next = head[u], head[u] = tot++;
		G[tot].to = u, G[tot].next = head[v], head[v] = tot++;
	}
	inline void built(int n) {
		int u, v;
		for (int i = 0; i < n - 1; i++) {
			scanf("%d %d", &u, &v);
			add_edge(u, v);
			vis[v] = true;
		}
		for (int i = 1; i <= n; i++) { if (!vis[i]) root = i; }
		memset(vis, false, sizeof(vis));
		scanf("%d %d", &A, &B);
	}
	inline int find(int x) {
		while (x != par[x]) {
			x = par[x] = par[par[x]];
		}
		return x;
	}
	inline void tarjan(int u, int fa) {
		for (int i = head[u]; ~i; i = G[i].next) {
			int &v = G[i].to;
			if (v == fa) continue;
			tarjan(v, u);
			vis[v] = true;
			par[v] = u;
		}
		if (u == A && vis[B]) { ans = find(B); }
		if (u == B && vis[A]) { ans = find(A); }
	}
	inline void solve(int n) {
		init(n);
		built(n);
		tarjan(root, root);
		printf("%d\n", ans);
	}
}go;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int t, n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		go.solve(n);
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4992905.html

NDIAurora 6D、5D传感器裸线焊接与烧录 6D传感器型号:10001742;5D传感器型号:610157 NDI Aurora 6D、5D传感器裸线焊接与烧录 阅读详情

相关推荐

人形机器人解耦驱动腿部设计与FAST性能优化

机器人运动控制的核心在于动力学解耦与能量效率优化。通过多连杆机构设计,可以实现关节运动的独立控制,显著降低惯性叠加和能量损耗。DecARt Leg采用创新的齿轮-四杆复合机构,将腿部伸缩与摆动运动完全解耦,配合多杆踝关节传动系统,实测行走速度可达4.18m/s。这种解耦设计不仅提升了动态性能,其提出的FAST指标更为腿部构型提供了量化评估基准,在仓储物流、地形适应等场景展现出显著优势,为人形机器人运动控制提供了新的技术路径。

weixin_33759613的博客 238

poj 1330 Nearest Common Ancestors 朴素LCA算法

Nearest Common AncestorsTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9213 Accepted: 4883DescriptionA rooted tree is a well-know

kongming_acm的专栏 848

解锁笔记智能对话:手把手配置 Obsidian Copilot 插件与硅基流动模型

本文详细介绍了如何为Obsidian笔记软件配置Copilot插件,并接入硅基流动AI模型,从而为个人知识库添加智能对话能力。通过手把手教程,指导用户完成插件安装、API Key获取、模型配置及索引构建,最终实现基于个人笔记内容的深度语义问答,提升知识管理效率。

corn8的博客 612

POJ Nearest Common Ancestors

最近公共祖先问题(Tarjan算法)

明天,你又将身在何方…… 988

【Telephony】APN配置及加载流程&项目实践(AOSP)

学习APN关键是学会明白apns-conf.xml文件各个参数的意思,然后熟悉TelephonyProvider是如何读取文件创建数据库的,譬如数据库表的约束、文件的读写顺序等,还有读取后的ApnSetting的二次定制以及显示(例如屏蔽xcap等)。虽然一般在项目中APN的需求很简单,就是按照客户提供的APN参数添加上配置,或者在显示屏蔽一些参数,但是项目维护的过程中APN的问题也是比较多的。

weixin_44513784的博客 5505

BK7238(XH-WB3S)固件烧录实战:从AT固件选择到稳定下载全解析

本文详细解析了BK7238(XH-WB3S)模组固件烧录的全过程,从AT固件版本选择到稳定下载的实战技巧。重点介绍了烧录前的准备工作、硬件连接的正确姿势、烧录软件的实战技巧以及疑难问题排查指南,帮助开发者高效完成BK7238模组的固件烧录。

weixin_29159441的博客 246

POJ_Nearest Common Ancestors

#include #include #include #include #include #include #include #include #include #include #include #pragma warning(dis

cxy7tv的博客 458

Nordic开发者必备:SEGGER Embedded Studio免费许可证申请与配置全攻略

本文为Nordic芯片开发者提供了SEGGER Embedded Studio(SES)集成开发环境的详细配置指南。内容涵盖免费许可证的合规申请流程、项目创建、Nordic SDK集成,以及编辑器优化、构建调试配置、RTT日志输出等核心功能的高效设置,旨在帮助开发者构建深度优化的专业开发环境,显著提升嵌入式开发效率。

cheese的博客 353

poj1330 Nearest Common Ancestors()

本文代码参考kuangbin:点击打开链接 本文解释参考:点击打开链接 题目链接:点击打开链接 题目大意:给出n 个结点和n-1条边构成一个生成树 然后查询第i和第j个节点的第一个公共结点 思路:LCA的裸题 对于该问题,最容易想到的算法是分别从节点u和v回溯到根节点,获取u和v到根节点的路径P1,P2,其中P1和P2可以看成两条单链表,这就转换成常见的一道面试题:【判断两

qq_36782366的博客 272

E-Prime脑电实验打mark全攻略:从端口查询到串并口实战(附常见问题解决)

本文提供了E-Prime脑电实验打mark的完整实战指南,涵盖端口查询、并口与串口配置的核心步骤及常见问题解决。文章深入解析了mark原理,并详细介绍了通过E-Basic脚本和Serial设备对象发送信号的两种方法,帮助研究者实现E-Prime与脑电设备的高精度时间同步,确保实验数据可靠性。

4k5l6j7h8的博客 354

Dify基础:Dify平台的本地化部署(含windows部署中,常见问题及解决方案)

第一种比较简单,只需要在浏览器中,打入https://dify.ai/就可以。这个指令虽然看起来简单,但实际上涉及到对docker的使用。这个项目既然有github地址,那它一般就是开源的。我们点开这个github地址,便能看到dify的私有化部署方式。我们可以看到,还是上面那个图,有个按钮,是指向该项目的Github地址的。第二步:按照dify的github给出的指令,对dify进行安装。第一步:我们把dify下载到自己的电脑(或者服务器)上。这里,我们只考虑第二种方式,也就是私有化部署。

xiezhipu的博客 1万+

POJ 1330 Nearest Common Ancestors

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13065   Accepted: 6969 Description A rooted tree is a well-known data structure in com

hqu_fritz的专栏 542

基于Python的雪球网投资组合数据爬取与分析实战:从入门到精通

在当今数字化投资时代,数据已成为投资者最重要的资产之一。雪球网作为中国领先的投资社交平台,汇聚了大量优质的投资组合数据,这些数据对于量化投资研究和个人投资决策具有重要价值。本文将详细介绍如何使用Python最新技术栈构建一个高效、稳定的雪球网投资组合爬虫,并深入分析获取的数据。投资组合持仓明细及变动历史投资收益率曲线与风险指标投资者操作策略与调仓频率热门行业与个股关注度。

2201_76125261的博客 920

LCA POJ 1330 Nearest Common Ancestors

POJ 1330Nearest Common Ancestors Time Limit:1000MS Memory Limit:10000K Total Submissions:24209 Accepted:12604 Description A rooted tree is a well-known data struct...

weixin_30911809的博客 97

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

POJ1330 Nearest Common Ancestors

POJ1330 筱柒Littleseven QQ:3534947775 题目链接 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN = 10010; int tot...

筱柒LittleSeven 196

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

poj 1330 Nearest Common Ancestors 【LCA】

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20073   Accepted: 10631 Description A rooted tree is a well-known data structure i

shengweisong 776

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【POJ 1330】 Nearest Common Ancestors

【题目链接】           点击打开链接【算法】         倍增法求最近公共祖先【代码】         #include &lt;algorithm&gt; #include &lt;bitset&gt; #include &lt;cctype&gt; #include &lt;cerrno&gt; #include &lt;clocale&gt; #include &lt;cmat...

even_bao的博客 210

POJ【1330】Nearest Common Ancestors

题目链接:http://poj.org/problem?id=1330 分析 这是一道LCA裸题,可以采用tarjan算法做,注意题目规定了父节点指向子节点,所以需要先找到根节点,从根结点开始往下搜。 #include<iostream> #include<cstdio> #include<vector> using namespace std; const ...

yj8023xx的博客 167

POJ 1330 Nearest Common Ancestors LCA

POJ 1330 可以说是经典的LCA求最近公共祖先的模板题,先入门感受下LCA,还有poj运行STL会WA,所以建议使用静态连接的邻接表做,还有codeforces的#include头文件也不支持。 代码如下: #include #include #include #include using namespace std; typedef long long LL; #define INF

Akahieveman的博客 571

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义。f[i][j]表示i节点的第2j个父亲是多少      这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2

Macan - 生活点滴 532

[POJ 1330] Nearest Common Ancestors (朴素方法)

POJ 1330: Nearest Common Ancestors Time Limit: 1000ms Memory Limit: 32Mb Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:...

weixin_30595035的博客 67

poj1330 Nearest Common Ancestors

LCA模板题

YHaX的博客 359

POJ1330 Nearest Common Ancestors【LCA+树】

Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40900 Accepted: 20173 Description A rooted tree is a well-known data structure in computer science and engineering...

海岛Blog 571
上一篇: poj 1564 Sum It Up
下一篇: hud 2586 How far away ?
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值