SP14932 LCA - Lowest Common Ancestor
Description:
一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树。\(-Wikipedia\)
最近公共祖先(\(LCA\))是……(此处省去对\(LCA\)的描述),你的任务是对一棵给定的树\(T\)以及上面的两个节点\(u,v\)求出他们的\(LCA\)。
例如图中9和12号节点的LCA为3号节点
Input:
输入的第一行为数据组数\(T\),对于每组数据,第一行为一个整数\(N(1\leq N\leq1000)\),节点编号从\(1\)到\(N\),接下来的\(N\)行里每一行开头有一个数字\(M(0\leq M\leq999)\),\(M\)为第\(i\)个节点的子节点数量,接下来有\(M\)个数表示第\(i\)个节点的子节点编号。下面一行会有一个整数\(Q(1\leq Q\leq1000)\),接下来的\(Q\)行每行有两个数\(u,v\),输出节点\(u,v\)在给定树中的\(LCA\)。
输入数据保证只有一个根节点并且没有环。
Output:
对于每一组数据输出\(Q+1\)行,第一行格式为\("Case i:"\)(没有双引号),\(i\)表示当前数据是第几组,接下来的\(Q\)行每一行一个整数表示一对节点\(u,v\)的\(LCA\)。
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_g\)l_
思路:一道求\(LCA\)的板子题,根据题目给出的每个点的孩子建边然后找出根结点,直接\(dfs\)求出深度后跑\(LCA\)就可以了。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 1007
using namespace std;
int t,q,rt,tim,f[maxn][22],n,m,head[maxn],d[maxn],num;
bool vis[maxn];
struct node {
int v,nxt;
}e[maxn<<1];
inline void ct(int u, int v) {
e[++num].v=v;
e[num].nxt=head[u];
head[u]=num;
}
void dfs(int u, int fa) {
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v!=fa) {
f[v][0]=u;
d[v]=d[u]+1;
dfs(v,u);
}
}
}
inline int lca(int a, int b) {
if(d[a]>d[b]) swap(a,b);
for(int i=20;i>=0;--i)
if(d[a]<=d[b]-(1<<i)) b=f[b][i];
if(a==b) return a;
for(int i=20;i>=0;--i)
if(f[a][i]!=f[b][i]) a=f[a][i],b=f[b][i];
return f[a][0];
}
int main() {
scanf("%d",&t);
while(t--) {
++tim;
memset(f,0,sizeof(f));
memset(d,0,sizeof(d));
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
num=0;
scanf("%d",&n);
for(int i=1,m;i<=n;++i) {
scanf("%d",&m);
for(int j=1,v;j<=m;++j) {
scanf("%d",&v);
ct(i,v);ct(v,i);
vis[v]=1;
}
}
for(int i=1;i<=n;++i) if(!vis[i]) rt=i;
dfs(rt,0);
for(int j=1;j<=20;++j)
for(int i=1;i<=n;++i)
f[i][j]=f[f[i][j-1]][j-1];
scanf("%d",&q);
printf("Case %d:\n",tim);
for(int i=1,u,v;i<=q;++i) {
scanf("%d%d",&u,&v);
printf("%d\n",lca(u,v));
}
}
return 0;
}
SP14932 LCA - Lowest Common Ancestor的更多相关文章
- 洛谷 SP14932 LCA - Lowest Common Ancestor
洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...
- SP14932 【LCA - Lowest Common Ancestor】
专业跟队形 唯一一个有$\LaTeX$的 裸的$LCA$,我用的是$Tarjan~LCA$,注意两点相同特判 #include<iostream> #include<cstdio&g ...
- 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)
转自 剑指Offer之 - 树中两个结点的最低公共祖先 题目: 求树中两个节点的最低公共祖先. 思路一: ——如果是二叉树,而且是二叉搜索树,那么是可以找到公共节点的. 二叉搜索树都是排序过的,位于左 ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
- Lowest Common Ancestor (LCA)
题目链接 In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defi ...
- PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
随机推荐
- javah生成带有包名的头文件
无包名情况 多数的demo都是基于这种条件,假设在目录jni/下有一个包含native方法的文件Hello.class.进入jni/目录,直接执行javah Hello,就可以在jni/目录下生成文件 ...
- 如何识别真Microsoft服务与非Microsoft服务来定位病毒自己的服务
在我当网管的那段时间,发现有病毒入侵客户服务器,该病毒伪装自己的进程名,我们在服务中发现其也有伪装成系统服务的服务在运行,占用客户服务器的性能,使得CPU与内存的利用率达到90%以上,并持续时间长,甚 ...
- 纯CSS3左右滑动开关按钮
纯CSS3特效左右滑动开关按钮是一款非常酷的CSS3 3D开关按钮,点击按钮可以左右滑动,就像开关打开闭合一样的效果. http://www.huiyi8.com/sc/10626.html
- ffmpeg给视频加文字水印
ffmpeg -i dd2800.mp4 -vf "drawtext=fontfile=Arial.ttf: text='Hu':x=100:y=10:fontsize=24:fontcol ...
- mysql修改初始密码
通过MySQL命令行,可以修改MySQL数据库的密码,下面就为您详细介绍该MySQL命令行,如果您感兴趣的话,不妨一看. 格式:mysqladmin -u用户名 -p旧密码 password 新密码 ...
- 1080 Graduate Admission (30)(30 分)
It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applicat ...
- Python调试指南
http://blog.sina.com.cn/s/blog_a15aa56901017u0p.html http://www.cnblogs.com/coderzh/archive/2009/12/ ...
- rt-thread的定时器管理源码分析
1 前言 rt-thread可以采用软件定时器或硬件定时器来实现定时器管理的,所谓软件定时器是指由操作系统提供的一类系统接口,它构建在硬件定时器基础之上,使系统能够提供不受数目限制的定时器服务.而硬件 ...
- Mysql MMM 高可用
一.Mysql MMM 高可用概况: mmm_mond 负责所有的监控工作的监控守护进程,决定节点的移除等: mmm_agentd 运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给 ...
- SQL大全(1)
实用SQL语句大全 一.基础 1.创建数据库 create database 数据库名 2.删除数据库 drop database 数据库名 3.增加列 alter table 表名 add 列名 类 ...