POJ 1470 Closest Common Ancestors 【LCA】

低代码组件开发——OpenClaw智能助手的组件设计与开发(2026技术版) OpenClaw智能助手的低代码组件开发平台采用分层架构设计,包含组件定义、注册、配置、渲染和生命周期管理等核心模块。该系统支持UI、功能、数据和集成四种组件类型,强调可重用性、可配置性和可扩展性。组件定义通过TypeScript、React等技术实现组件结构和行为的描述,组件注册中心基于Node.js和MongoDB管理组件版本。该平台通过React、Redux等技术实现组件渲染和状态管理,并提供完整的生命周期管理机制。这种架构设计显著提升了智能助手应用的开发效率和质量。 阅读详情

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

Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 22519 Accepted: 7137

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form: 

nr_of_vertices 
vertex:(nr_of_successors) successor1 successor2 ... successorn 
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ... 

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times 
For example, for the following tree: 

Sample Input

5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
      (2 3)
(1 3) (4 3)

Sample Output

2:1
5:5

Hint

Huge input, scanf is recommended.

 

题意概括:

给一棵 N 个结点 N-1 条边的树,和 M 次查询;

形式是:先给根结点 然后 给与这个根结点相连的 子节点。

查询的对儿给得有点放荡不羁,需要处理一下空格、回车、括号。。。

解题思路:

虽说表面上看是一道裸得 LCA 模板题(简单粗暴Tarjan)

但是细节还是要注意:

本题没有给 查询数 M 的范围(RE了两次)所以要投机取巧一下不使用记录每对查询的 LCA。

本题是多测试样例,注意初始化!!!

 

AC code:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <vector>
  6 #define INF 0x3f3f3f3f
  7 #define LL long long
  8 using namespace std;
  9 const int MAXN = 1e3+10;
 10 struct Edge{int v, w, nxt;}edge[MAXN<<1];
 11 struct Query
 12 {
 13     int v, id;
 14     Query(){};
 15     Query(int _v, int _id):v(_v), id(_id){};
 16 };
 17 vector<Query> q[MAXN];
 18 
 19 int head[MAXN], cnt;
 20 int fa[MAXN], ans[MAXN<<1], no[MAXN];
 21 bool vis[MAXN], in[MAXN];
 22 int N, M;
 23 
 24 void init()
 25 {
 26     memset(head, -1, sizeof(head));
 27     memset(in, false, sizeof(in));
 28     memset(vis, false, sizeof(vis));
 29     //memset(ans, 0, sizeof(ans));
 30     memset(no, 0, sizeof(no));
 31     cnt = 0;
 32     for(int i = 0; i <= N; i++)
 33         fa[i] = i, q[i].clear();
 34 }
 35 
 36 int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
 37 
 38 void AddEdge(int from, int to)
 39 {
 40     edge[cnt].v = to;
 41     edge[cnt].nxt = head[from];
 42     head[from] = cnt++;
 43 }
 44 
 45 void Tarjan(int s, int f)
 46 {
 47     int root = s;
 48     fa[s] = s;
 49     for(int i = head[s]; i != -1; i = edge[i].nxt)
 50     {
 51         int Eiv = edge[i].v;
 52         if(Eiv == f) continue;
 53         Tarjan(Eiv, s);
 54         fa[getfa(Eiv)] = s;
 55     }
 56     vis[s] = true;
 57     for(int i = 0; i < q[s].size(); i++){
 58         //if(vis[q[s][i].v]) ans[q[s][i].id] = getfa(q[s][i].v);
 59         if(vis[q[s][i].v])
 60             no[getfa(q[s][i].v)]++;
 61     }
 62 }
 63 
 64 int main()
 65 {
 66     while(~scanf("%d", &N)){
 67         init();
 68         //scanf("%d", &N);
 69         for(int i = 1, u, v, k; i <= N; i++){
 70             scanf("%d:(%d)", &u, &k);
 71             for(int j = 1; j <= k; j++){
 72                 scanf("%d", &v);
 73                 AddEdge(u, v);
 74                 in[v] = true;
 75             }
 76         }
 77         int root = 0;
 78         for(int i = 1; i <= N; i++) if(!in[i]){root = i;break;}
 79         scanf("%d", &M);
 80         int sum = 1, u, v;
 81         while(sum <= M){
 82             while(getchar()!='(');
 83             scanf("%d%d", &u, &v);
 84             while(getchar()!=')');
 85             q[u].push_back(Query(v, sum));
 86             q[v].push_back(Query(u, sum));
 87             sum++;
 88         }
 89         Tarjan(root, -1);
 90         /*
 91         for(int i = 1; i <= M; i++){
 92             no[ans[i]]++;
 93         }
 94         */
 95 
 96         for(int i = 1; i <= N; i++){
 97             if(no[i]) printf("%d:%d\n", i, no[i]);
 98         }
 99     }
100     return 0;
101 }

 

使用 SAP 自家的 AI 工具 Joule 辅助 SAP Fiori 应用开发 SAP Joule 不请自答,解释说标记了接口的 UI Component,具备进行异步内容创建的能力,比如其通过 rootView 分配的视图,在运行时可以异步创建。目前 SAP Business Application Studio 里的 SAP Joule 支持下面这八种上下文,绝大部分都和 SAP UI5 开发相关,剩下的则是代码仓库和 SAP 帮助文档的搜索。SAP Joule 回答的大意是:我只负责回答 SAP UI5 开发相关的问题,不要和我扯其他有的没的。 阅读详情

相关推荐

从病毒分析到安全研究:手把手教你用OD破解加壳程序的反调试机制

本文深入探讨了如何识别并绕过加壳程序的反调试机制,特别是针对使用IsDebuggerPresent等API检测的防护手段。通过实战案例和工具链配置,详细介绍了逆向工程中的关键技巧,包括内存补丁、硬件断点定位OEP等,帮助安全研究人员有效突破软件保护技术。

tgb345678的博客 825

poj 1470 Closest Common Ancestors 【Tarjan 离线 LCA】

题目:poj 1470 Closest Common Ancestors 题意:给出一个树,一些询问。求LCA的个数、 分析:很简单的模板题目,但是模板不够优秀,一直wa...RE,各种错误一下午,终于发现自己模板的漏洞了。 AC代码: #include #include #include #include using namespace std; #def

大神养成中..... 1649

EB Tresos激活失败?3种实用解决方案帮你搞定(含离线激活步骤)

本文针对EB Tresos软件激活失败问题,提供了三种实用的解决方案。详细分析了激活失败的常见根源,并系统阐述了在线环境下的排查步骤、无网环境下的离线激活全流程,以及高级许可证环境修复方法,帮助汽车电子工程师快速恢复开发环境。

milk8的博客 609

POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest...

dipinzhu4111的博客 245

FOC控制必看:单/双/三电阻电流采样方案全对比(附NXP实战避坑指南)

本文深入对比了FOC控制中单电阻、双电阻和三电阻电流采样方案的优劣与适用场景,重点剖析了采样时序、窗口与ADC转换的工程挑战。针对NXP平台,提供了解决采样窗口过窄的实战方案与配置指南,帮助工程师根据成本与性能需求做出最优选择,并规避常见设计陷阱。

n2o3p4的博客 416

VC Spyglass:Lint实战指南之TCL脚本自动化解析

本文详细介绍了如何利用TCL脚本实现VC Spyglass Lint检查的自动化流程。通过实战指南,从基础脚本编写到高级参数化技巧,帮助数字IC设计工程师摆脱手动操作,提升验证效率,并实现与CI/CD流程的集成。

h3i4j的博客 799

poj 1470 Closest Common Ancestors(LCA)

poj 1470 Closest Common Ancestors 基础的LCA,但是输入ha

yjCola 559

YOLO11改进|注意力机制篇|引入ELA注意力机制

YOLO11添加ELA注意力机制

A1983Z的博客 3648

POJ 1470 Closest Common Ancestors【LCA Tarjan】

题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数。 分析: 还是很裸的tarjan的LCA。 这题我真的要吐槽!!调试了好久啊!!不是MLE就是RE。。。。 查询数量没给,所以要用矩阵来存,这样还可以避免重复。 给边的时候不知道会不会有重复的...

An55511的博客 205

Serial RapidIO Gen2 IP 说明(一)

概述 高速IO(RapidIO)互联架构,用来兼容当前最流行的集成通信处理器、主处理器、联网的数字信号处理器,是一种高性能、分组交换(报文交换)的互联技术。它解决了高性能嵌入式产业稳定性、高带宽以及系统内部更快的总线速率的需求。 高速IO(RapidIO)标准由三层定义:逻辑层、传输层、物理层。逻辑层定义了所有的协议内容和报文格式。这些是终端发起和完成一次传输所必需的信息。传输层提供了报文从一个端点传输到另外一个端点必要的路线信息。物理层则描述了设备级的端口声明,如报...

记录所学,分享知识,结识挚友 1497

Python 爬虫实战:爬虫代码打包为可执行文件

本文详细介绍了使用PyInstaller工具将Python爬虫打包为可执行文件的全过程。以豆瓣电影Top250爬虫为例,首先对比了主流打包工具,推荐选择跨平台的PyInstaller。通过编写完整的爬虫代码(包含异常处理和数据保存功能),演示了从环境准备、代码测试到打包配置的具体步骤。重点讲解了单文件打包、自定义图标等高级功能,并针对常见的"找不到模块"、文件体积过大等问题提供了解决方案。文章还说明了跨平台打包的注意事项,帮助开发者实现爬虫程序的无环境依赖部署,大幅提升程序的易用性和部署

2503_91057718的博客 2216

[POJ 1470]Closest Common Ancestors[离线LCA]

题目链接:[POJ 1470]Closest Common Ancestors[离线LCA] 题意分析: 给出多个查询,查询点u和v的最近公共祖先。输出每个点作为最近公共祖先在查询中出现的次数,0次的不输出。 解题思路: 离线LCA,需要用到tarjan。 和普通的tarjan差不多。多了两样东西: 1.u - > v回溯的时候,更新v的祖先为u 2.在递归结尾,检查查询。 个人感

闲停 693

RDKit | 基于Pytorch和RDKit建立QSAR模型

QSAR 定量构效方法(quantitative structure-activity relationship, QSAR)是应用最为广泛的药物设计方法。所谓定量构效方法就是通过一些数理统计方法建立其一系列化合物的生理活性或某种性质与其物理化学性质之间的定量关系,通过这些定量关系。可以预测化合物的生理活性或某些性质,指导我们设计出具有更高活性的化合物。 早在...

DrugOne 5079

【LCA基础】poj 1470 Closest Common Ancestors

LCA裸题

Gzbgreat的博客 468

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

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

poj1470 Closest Common Ancestors(LCA离线优化)

http://poj.org/problem?id=1470 题意:给你点和关系,还有m次询问,求某点是最小公共祖先的次数。 思路:整体思路和poj1330相似,但是这题时间卡的相当紧,听别人说是多组询问。解决办法就是用数组存起来,LCA的时候每处理一个点就处理与该点有关的询问,这样遍历完也就处理完所有询问。保存次数我用的结构体,刚开始想复杂了。。还有输入输出格式也比较恶心,处理完

Iguodala的博客 695

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

poj1470——Closest Common Ancestors//LCA

<br />果然A得辛苦,我想是用错模板了。囧,好累。http://www.cppblog.com/abilitytao/archive/2009/09/21/96886.html<br />#include<iostream> #include<string> #include<cstdio> #include<vector> using namespace std; const int maxn=906; vector<int >p[maxn]; class node { public

r_ax的专栏 585

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

poj1470-Closest Common Ancestors(LCA)

Closest Common Ancestors Time Limit:2000MS     Memory Limit:10000KB      Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the pro

AC之路 1407

POJ 1470 Closest Common Ancestors tarjan算法求LCA

题目:http://poj.org/problem?id=1470 题意:输入一个n,代表树有n个点,从1到n。接下来n行,每行第一个数为树上的点,然后一个数,假设是m吧,其后跟m个数,代表之前的点指向这m个点。然后有大于等于1组的查询,查询它们的LCA,最后输出每个点作为LCA的次数,为0的不输出。 思路:LCA模板题啦,求LCA时直接统计一下就好了,输入有点小恶心 #in

弱智就要多努力 1085

POJ 1470 Closest Common Ancestors LCA题解

本题也是找LCA的题目,只是要求多次查询。一般的暴力查询就必定超时了,故此必须使用更高级的方法,这里使用Tarjan算法。 本题处理Tarjan算法,似乎输入处理也挺麻烦的。 注意: 由于查询的数据会极大,故此使用一个数组记录全部查询数据就会超时的。我就载在这里了。查了好久才想到这点。由于我使用了一个vector容器记录了查询数据。故此每次都循环这组这么大的数据,就超时了。-...

aofan9566的博客 164

POJ-1470 Closest Common Ancestors【LCA】

题目链接:http://poj.org/problem?id=1470 题目大意: 给你一棵树,然后求公共祖先。 只不过我们需要记录每个节点作为公共祖先的次数。然后升序输出。 解题思路: 这道题,我不想做啥评论了。。。。。。。。。你妹啊!!!!!!!!!为了你,WA了20多次啊啊啊啊啊啊啊啊啊啊啊啊!!!!!!!! 原来Wrong的代码,数组开大一点点就过了啊。。。。。。。原来

飘过的小牛 2448

【LCA最近公共祖先】 poj1470 Closest Common Ancestors

It's a shame that I have wasted too much time on it... But it's just a simple LCA problem, and we can use tarjan algorithm(O(n+q)) to solve it... I have code a array name "head2" to "head" without n

When you go away 705

POJ - 1470 Closest Common Ancestors (LCA tarjan)

POJ - 1470 题目 思路:tarjan #include #include #include #include #include #include #include using namespace std; const int maxn = 1e3+10; vector h[maxn]; vector query[maxn]; int f[maxn], anc[max

shinimashzi 255

Closest Common Ancestors【POJ1470】——LCA

Time Limit: 2000MS Memory Limit: 10000KDescriptionWrite a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common a

绝尘 1021

POJ1470 Closest Common Ancestors LCA

依然是没什么难度的模板题..不过查询的时候,没给上限.数组开小就就RE了= =,还有就是根节点要自己找一下.. #include #include #include #include #include using namespace std; typedef long long ll; const int maxn=1000; int n,m,p; struct EDGE {

Broken Wings 808

POJ 1470 Closest Common Ancestors LCA离线Tarjin算法

http://poj.org/problem?id=1470 题意:给定一颗有根树,并且有Q次询问,格式是(u,v),求任意一次询问的两个结点的最近公共祖先。 算法:裸的LCA ,这是LCA的第一题,首先说下LCA离线Tarjin算法,其实质就是dfs +并查集。首先将所有的查询都先记录下面(因为是离线算法),然后就是从根结点对树进行一次dfs,复杂度为O(E) ,注意树用邻接矩阵存储。当

Chris_Home 642

POJ 1470 Closest Common Ancestors [LCA+RMQ]

这题的输入有点恶心,OLE很多次才过。。。 ST:#include #include #include #include #include #include using namespace std; const int maxn = 10010; int n, q, cnt; vector son[maxn]; int parent[maxn]; bool vis[maxn];

~飞呀,飞~ 604
上一篇: CODEVS 2370 小机房的树
下一篇: POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值