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的更多相关文章

  1. 洛谷 SP14932 LCA - Lowest Common Ancestor

    洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...

  2. SP14932 【LCA - Lowest Common Ancestor】

    专业跟队形 唯一一个有$\LaTeX$的 裸的$LCA$,我用的是$Tarjan~LCA$,注意两点相同特判 #include<iostream> #include<cstdio&g ...

  3. 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)

    转自 剑指Offer之 - 树中两个结点的最低公共祖先 题目: 求树中两个节点的最低公共祖先. 思路一: ——如果是二叉树,而且是二叉搜索树,那么是可以找到公共节点的. 二叉搜索树都是排序过的,位于左 ...

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

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

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

  7. Lowest Common Ancestor (LCA)

    题目链接 In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defi ...

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

  9. [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 ...

随机推荐

  1. Android4.4 GPS框架分析【转】

    本文转载自:http://blog.csdn.net/junzhang1122/article/details/46674569 GPS HAL层代码在目录trunk/Android/hardware ...

  2. hadoop —— MapReduce例子 (数据去重)

    参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...

  3. Codeforces Round #551 (Div. 2) A~E题解

    突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...

  4. 算法(Algorithms)第4版 练习 1.3.20

    方法实现: //1.3.20 /** * delete the kth element in a linked list, if it exists. * * @param k the kth ele ...

  5. VLAN虚拟局域网技术(三)-计算机网络

    本文主要知识来源于学校课程,部分知识来自于H3C公司教材,未经许可,禁止转载.如需转载,请联系作者并注明出处. 本节主要介绍 pVLAN和 动态VLAN. 1.   pVLAN:英文全称Private ...

  6. python之tkinter_2

    关于tkinter的组件介绍,这篇博客很详细: https://www.cnblogs.com/aland-1415/p/6849193.html tkinter is to make interfa ...

  7. SFTP 文件上传下载引用代码

    http://sha1064616837.iteye.com/blog/2036996 http://www.cnblogs.com/itmanxgl/p/fe5d33512609fe540eb08a ...

  8. zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)

    1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...

  9. listen 58

    Different Brain Regions Handle Different Music Types (Vivaldi) versus (the Beatles) . Both great. Bu ...

  10. Linux 中安装软件报缺少共享库文件的错误

    linux 安装一些如软件 或者相关的模块时,经常报出缺少一些依赖包的 libxxx.so等的共享库文件 首先找到 该共享库文件 然后查看/etc/ld.so.conf 这个文件记录了编译时使用的动态 ...