POJ 1330 Nearest Common Ancestors 【LCA模板题】

Python实战:用SkillMetrics库5分钟搞定泰勒图绘制(附完整代码) 本文详细介绍了如何使用Python的SkillMetrics库快速绘制专业泰勒图,以直观对比多个模型的预测性能。通过整合相关系数、标准差和中心化均方根误差三个核心指标,该工具能帮助数据科学家和研究者高效完成模型评估与可视化,提升分析报告质量。文章提供了从环境配置、数据准备到高级定制与子图对比的完整代码示例。 阅读详情

任意门: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

 

基于Keil软件实现对射式红外传感器计次 (江协科技HAL库) 用HAL库复现江协科技,让嵌入式学习之路更顺畅。 阅读详情

相关推荐

手把手教你用Simulink搭建级联H桥储能变流器仿真模型(附SOC均衡分析)

本文详细介绍了如何使用Simulink搭建级联H桥储能变流器仿真模型,并附有SOC均衡分析。从仿真环境准备、单相H桥单元建模到多模块级联方案和SOC均衡策略实现,逐步指导读者完成复杂系统的构建与优化。通过关键仿真结果分析和工程实践问题排查,帮助读者掌握级联H桥储能变流器的核心技术与应用方法。

weixin_42524226的博客 448

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

多GPU时代的虚拟内存革命:CUDA VMM API的跨设备协同设计哲学

本文深入探讨了NVIDIA CUDA VMM API在多GPU系统中的虚拟内存管理革命,解决了传统Peer Access的显存碎片化和同步开销问题。通过地址-内存解耦、按需映射和精细权限控制,VMM API为分布式训练提供了高效的内存协同方案,显著提升大模型训练性能。

weixin_29251337的博客 539

POJ_Nearest Common Ancestors

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

cxy7tv的博客 461

如何让Arduino IDE支持你手里的STM32 nucleo开发板

Arduino IDE开发环境在默认情况下不支持STM32 NUCLEO开发板,只有通过添加附加开发板管理包才能使用。目前为止,只有一部分板子支持使用Arduino IDE开发环境进行开发,分别是 Nucleo F207ZG, Nucleo F429ZI, Nucleo F030R8, Nucleo F091RC, Nucleo F103RB, Nucleo F303RE, Nucl...

mzw72188552的专栏 8978

人工智能,不止于模型:四步实现完整工作流

工程师越来越多地致力于将人工智能 (AI) 集成到自己的项目和应用中,同时不断着力提升自己的 AI 技能。面对 AI 问题,工程师首先要了解什么是 AI,以及如何将它纳入当前工作流,这看似简单,实则未必容易。在 Google 中搜索“什么是 AI?”会得到数百万条结果,包括不同程度的技术和相关信息。那么,对工程师而言,AI 意味着什么?人们对 AI 的关注主要集中在 AI 模型上,受此影响,工程师会迅速投入 AI 建模的研究。

r1832644594的博客 2832

POJ Nearest Common Ancestors

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

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

金融数字化——详解2025年金融智能体深度应用报告【附全文阅读】

《2025年金融智能体深度应用报告》显示,金融智能体已实现80%部署率,推动行业效率提升30%-50%。报告指出智能体通过"感知-推理-规划-执行-进化"机制突破大模型局限,在风控、客服、投研等100+场景实现自动化。典型案例显示,工商银行信贷审批效率提升60%,蚂蚁数科智能客服解决80%问题。尽管面临系统协同、标准缺失等挑战,未来趋势将向多模态交互、自主进化和ESG投资等方向拓展。

cdfunlove的博客 210

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

SAP CPI 之使用国密算法实现SM系列算法加解密

System.out.println("SM2-私钥:" + privateKey);System.out.println("SM2-公钥:" + publicKey);System.out.println("SM2-密文:" + encrypts);System.out.println("SM2-明文:" + data2);新建groovy,通过import之后,调用jar包中的方法。在CP中导入jar包,选择Upload,选中导入的jar包。的密钥和向量值,不安全!的公钥和私钥,不安全!

u012830826的博客 1340

2020计算机专业保研夏令营面经:中科院计算所网数机试题目

计算所网络数据科学与技术重点实验室的第一阶段是机试。机试不限程序语言,要求提交源代码。

乔卿的博客 2961

poj1330 Nearest Common Ancestors()

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

qq_36782366的博客 272

【多模态实验总结】整理了几十篇文章,理清 多模态目标检测 在写论文中需要做哪些实验

此篇文章中将多模态目标检测所做的实验,分成了各个类别,目的是为了对多模态的实验有一个清晰的理解。可以看到,很多表格中放的并不是做了单一的某一个对比,而是将提出的模型,在不同的骨干、数据集上的效果与基线进行对比,使内容更全面。因此这里提到的各种实验,大家在后续改进写论文过程中可以根据自己的论文内容进行整合。对于这里提到的这些文章,大家都可以看看,我只是按照我这里的分类截取了部分实验,这里提到的所有实验在每篇文章中也基本能看到相应的实验。

Limiiiing的博客 3157

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

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

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

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的博客 209

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【LCA模板题】poj 1330 Nearest Common Ancestors

LCA模板题

Gzbgreat的博客 399

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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的博客 393

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的博客 426

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的专栏 1141
上一篇: 牛客国庆集训派对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、付费专栏及课程。

余额充值
⌂ 换网址