POJ 1330 Nearest Common Ancestors 【LCA模板题】

5步搞定ABB机器人RobotStudio与S7-1500PLC的Profinet通讯(附PLCSIM调试技巧) 本文详细介绍了如何利用RobotStudio与TIA Portal软件,结合PLCSIM Advanced仿真器,在零硬件条件下实现ABB机器人与西门子S7-1500 PLC之间的Profinet通讯虚拟调试。通过五个核心步骤,从环境搭建、网络配置、信号映射到联合仿真与高级调试,帮助工程师在项目前期高效验证逻辑、压缩现场调试周期,并掌握实用的PLCSIM调试技巧。 阅读详情

任意门:http://poj.org/problem?id=1330

Nearest Common Ancestors

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 34942 Accepted: 17695

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

 

题意概括:

给一棵有N个节点,N-1条边的树 和 一对结点,求这对结点的最近公共祖先。

解题思路:

找根结点用一个标记数组

找公共祖先用简单粗暴的 Tarjan。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #define INF 0x3f3f3f3f
 7 #define LL long long
 8 using namespace std;
 9 const int MAXN = 1e4+5;
10 struct Edge{int v, next;}edge[MAXN<<1];
11 int head[MAXN], cnt;
12 int fa[MAXN];
13 bool in[MAXN];
14 bool vis[MAXN];
15 int N, M, S, ans, a, b;
16 
17 inline void init()
18 {
19     memset(head, -1, sizeof(head));
20     memset(vis, false, sizeof(vis));
21     memset(in, false, sizeof(in));
22     cnt = 0;
23 }
24 
25 inline void AddEdge(int from, int to)
26 {
27     edge[cnt].v = to;
28     edge[cnt].next = head[from];
29     head[from] = cnt++;
30 }
31 
32 int findset(int x)
33 {
34     int root = x;
35     while(fa[root] != root) root = fa[root];
36 
37     int tmp;
38     while(fa[x] != root){
39         tmp = fa[x];
40         fa[x] = root;
41         x = tmp;
42     }
43     return root;
44 }
45 
46 void Tarjan(int s)
47 {
48     fa[s] = s;
49     for(int i = head[s]; i != -1; i = edge[i].next){
50         int Eiv = edge[i].v;
51         Tarjan(Eiv);
52         fa[findset(Eiv)] = s;
53     }
54     vis[s] = true;
55     if(s == a){
56         if(vis[a] && vis[b]) ans = findset(b);
57     }
58     else if(s == b){
59         if(vis[a] && vis[b]) ans = findset(a);
60     }
61 }
62 
63 int main()
64 {
65     int T_case, u, v;
66     scanf("%d", &T_case);
67     while(T_case--)
68     {
69         init();
70         scanf("%d", &N);
71         M = N-1;
72         for(int i = 1; i <= M; i++){
73             scanf("%d %d", &u, &v);
74             AddEdge(u, v);
75             in[v] = true;
76             //AddEdge(v, u);
77         }
78         scanf("%d %d", &a, &b);
79         int root = 0;
80         for(int i = 1; i <= N; i++){
81             if(!in[i]){root = i;break;}
82         }
83         Tarjan(root);
84         printf("%d\n", ans);
85     }
86     return 0;
87 }
View Code

 

UWB定位进阶:如何利用DW1000的CIR数据做NLOS信号识别? 本文深入探讨了如何利用DW1000芯片的信道脉冲响应(CIR)数据进行非视距(NLOS)信号识别,以提升UWB定位精度。通过分析CIR数据的核心价值、预处理方法、特征提取技术以及机器学习模型构建,为研究人员和工程师提供了实用的解决方案,帮助克服室内定位中的NLOS挑战。 阅读详情

相关推荐

图形学基础|球谐光照(Spherical Harmonics Lighting)

球谐光照(Spherical Harmonics Lighting) 文章目录球谐光照(Spherical Harmonics Lighting)一、前言二、球谐函数2.1 基函数2.2 投影与重建2.3 应用三、漫反射环境光3.1 IrradianceMap3.2 数学推导3.3 实践参考博文 一、前言 在学习图形渲染的过程中,一直對球谐函数(球谐光照)有一点了解,但没有亲手实现过。 关于球谐函数的数学概念是比较复杂的,笔者也实在无法完全理解,这篇文章只能从做东西的角度来说,公式是如何和代码进行对应的。

【黑键】 1万+

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

2024最新版:MacOS Sonoma系统下CocoaPods安装全攻略(解决Ruby版本冲突)

本文详细介绍了在MacOS Sonoma系统下安装CocoaPods的全过程,重点解决Ruby版本冲突问题。通过HomeBrew安装最新Ruby版本,配置环境变量,并正确安装CocoaPods,帮助iOS开发者顺利管理项目依赖。文章还提供了常见问题的解决方案和优化配置建议,确保开发环境的稳定性和高效性。

lll78的博客 1042

POJ_Nearest Common Ancestors

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

cxy7tv的博客 460

VirtualLab初学者教程-1.用户界面

目录前言一、VirtualLab Fusion简介1.软件2.内置工具箱:二、用户界面认识1.上方菜单栏2.右侧面主窗口3.底部窗口附 前言 感觉做VirtualLab的资料不是特别多,可能用的朋友也比较少吧,笔者最近也因为大创需要刚刚开始入门,把网上有的英文资料整理了一下,加了一点自己的内容发出来希望帮助更多在学习中的朋友,有用词不当之处还请多多包涵。 一、VirtualLab Fusion简介 因为是第一讲,简单啰嗦一下软件本身,参考官方介绍。 1.软件 VirtualLab是由德国LightTran

ClaireTang1的博客 2万+

matlab球面投影(二)

matlab球面投影,想做一个全景相机,不知道,要做成啥样,于是在MATLAB先仿真一下,看下是个啥样子 百度别人做的,复制下面的代码到MATLAB的命令串口下,,网上找了张全景图,可以是bmp,jpg,改程序就可以了 回车运行就可以看到结果。 复制下面代码时,下面的中文注释记得删除,MATLAB是会报错的, close all; clear all; clc [x,y,z]=sphere(50); A=imread('a.JPG'); //名字是自己命名的,记得改 B=warp(x,y,-z,

rjszcb的博客 3165

POJ Nearest Common Ancestors

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

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

CH569 USBSS库函数分析

CH569 USB3.0 解析

havc_W的博客 7686

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

WPS用户看过来:避开格式错乱坑,用WPS Office完美制作红头文件(2024最新版)

本文详细解析了WPS Office与Word在红头文件制作中的核心差异,提供了2024年最新版的零误差实战指南。从字体处理、页面布局到红头制作关键步骤,帮助用户避开格式错乱坑,确保跨设备显示一致。特别针对WPS用户优化,解决字体丢失、版式错位等常见问题。

weixin_26800111的博客 218

AEJoy —— 表达式之仅限在某一维摆动【JS】

效果图 wiggle() 的作用仅限于水平方向 想法 wiggle() 确实是一个非常方便的工具,但有时您希望对它有更多的控制。在本例中,我们希望仅将摆动限制在水平方向。 设计 要理解 wiggle() 最重要的一点是,它生成的结果与应用它的属性具有相同的维度。例如,如果你将 wiggle() 应用于 3D 层的 position 属性,结果将是一个由三个元素组成的数组(x、y 和 z 各一个)。我们需要做的就是将 wiggle() 的结果捕捉到一个变量(我们称之为 “w” ),然后从这个变量中提取我们想

ShaderJoy 的兴趣技术杂货铺 2095

poj1330 Nearest Common Ancestors()

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

qq_36782366的博客 272

【Python爬虫(1)】专栏开篇:夯实Python基础

要想熟练掌握 Python 爬虫技术,扎实的 Python 基础是必不可少的。这就好比建造高楼大厦,Python 基础就是那稳固的基石。只有打好了这个基础,我们才能在爬虫的学习道路上稳步前行,理解并运用各种爬虫技术和框架。

邓邓子的博客 2026

POJ 1330 Nearest Common Ancestors (LCA模板题)

题目 最裸的LCA模板题 分析 可以做的方式很多 离线 Tarjian算法 在线 LCA倍增法 在线 dfs + ST表算法 下面给出 ①:Tarjian + 邻接表, (POJ不支持万能头文件,和c++11,所以下面代码要修改才能提交) #include <bits/stdc++.h> #pragma GCC diagnostic error "-std=c++11" #defi...

Stormjing的博客 294

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

POJ 1330 Nearest Common Ancestors【tarjan】【简单LCA】【模板题】【并查集】

题目链接:http://poj.org/problem?id=1330 题目大意:给出n个父子关系,不会出现回路,求最原始祖先(根节点)。 照大佬的板子自己手码了一遍,还挺长,,, AC代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include&...

weixin_43499182的博客 208

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

【LCA模板题】poj 1330 Nearest Common Ancestors

LCA模板题

Gzbgreat的博客 399

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

poj1330 Nearest Common Ancestors(lca模板题)

题目 T组样例,N(2<=N<=1e4)个节点, 给出N-1条有向边u->v后,询问一组u和v的lca 思路来源 《挑战程序竞赛》P330-331 题解 lca裸题,用RMQ+ST就好了,其实倍增也是应该掌握的 由于只有一组询问,所以预处理欧拉序dfn id用来记录dfs序中第一次访问的时间戳 不妨id[u]<id[v],那么lca(u,v)=dfn[[i...

小衣同学的博客 300

【POJ - 1330】Nearest Common Ancestors(lca,模板题)

题干: 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 th...

xuanweiace的博客 391

POJ-1330 Nearest Common Ancestors(lca模板题)

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

yo_bc的博客 424

POJ 1330(Nearest Common Ancestors-Lca模板题)

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

nike0good |Oier&ACMer | 熟能生巧 1506

POJ 1330 Nearest Common Ancestors (LCA模板题)

题目链接:http://poj.org/problem?id=1330 #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;algorithm&gt; using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)&l...

满月照亮的路 246

poj-1330 Nearest Common Ancestors(LCA模板题)

题目链接:  http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23822   Accepted: 12405 Description

TUT我好菜啊 398

POJ-1330-Nearest Common Ancestors(LCA+倍增模板题)

题目链接:http://poj.org/problem?id=1330 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 wit...

永远鲜红の幼月的博客 168

F - Nearest Common Ancestors POJ - 1330 (LCA模板题)

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 roo...

立志变成java巨头 217

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

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

Macan - 生活点滴 533

POJ 1330:Nearest Common Ancestors ← 暴力法(同步前进法‌)+ LCA

“同步前进法‌”是一种求 LCA 的暴力法,即将深度较深的节点提升至与另一节点相同深度后,两节点同步向上移动,直到相遇的节点即为 LCA‌。例如,若节点 x 深度大于 y 的深度,则先将 x 提升至与 y 同深度,再共同向上搜索‌。

hnjzsyjyj的专栏 1139
上一篇: 牛客国庆集训派对Day2 F、平衡二叉树 【构造+记忆化搜索】
下一篇: HDU 2586 How far away ?【LCA】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值