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. JS工具函数汇总

    备注:http://phpjs.org/  这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...

  2. sql中使用timestamp增量抽取数据

    网址:http://www.cnblogs.com/shuaifei/p/4469526.html 最近的项目中需要对上百万级的数据进行增量抽取操作,因此了解了一下TIMESTAMP的应用,特此记录 ...

  3. Java常用类Date、Calendar、SimpleDateFormat详解

    Date类 java.util 包提供了 Date 类来封装当前的日期和时间,Date 类提供两个构造函数来实例化 Date 对象 第一个构造函数使用当前日期和时间来初始化对象   Date( ) 第 ...

  4. svn 操作手册

    1.执行下列指令安装    sudo apt-get install subversion   2.  创建版本库       sudo mkdir /home/svn    sudo mkdir / ...

  5. 字典树Trie的使用

    1. Trie树介绍 Trie,又称单词查找树.前缀树,是一种多叉树结构.如下图所示: 上图是一棵Trie树,表示了关键字集合{“a”, “to”, “tea”, “ted”, “ten”, “i”, ...

  6. KCF+Opencv3.0+Cmake+Win10 测试

    配置 需要的文件下载 安装CMake,安装opencv3.0.0 在KCFcpp-master 目录下新建一个文件夹,命名为build 打开CMake-GUI配置如下: 点击Configure,编译器 ...

  7. win7-64 mysql的安装

    1.https://jingyan.baidu.com/article/597035521d5de28fc00740e6.html 2.net start mysql 无法启动的3534的错误的解决办 ...

  8. 机器学习、图像识别方面 书籍推荐 via zhihu

    机器学习.图像识别方面 书籍推荐 作者:小涛 链接:https://www.zhihu.com/question/20523667/answer/97384340 来源:知乎 著作权归作者所有.商业转 ...

  9. 使用superobject 解析Json数据

    接口数据有如下规范{"error": 0, "msg": "", "data": ...} 其中数据data类型不确定. ...

  10. [bzoj2301]Problem b莫比乌斯反演+分块优化

    题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...