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. POJ2104 K-th Number (子区间内第k大的数字)【划分树算法模板应用】

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 40920   Accepted: 13367 Ca ...

  2. 为什么在实际的 kaggle 比赛中 gbdt 和 random forest 效果非常好?

    https://www.zhihu.com/question/51818176/answer/127637712

  3. Sysctl命令及linux内核参数调整

        一.Sysctl命令用来配置与显示在/proc/sys目录中的内核参数.如果想使参数长期保存,可以通过编辑/etc/sysctl.conf文件来实现.    命令格式:  sysctl [-n ...

  4. (转)[sh] <defunct>进程

    我也碰到僵尸进程了,必须通过关闭僵尸进程的父进程或者重启解决. 来自:http://jerrybear.blog.51cto.com/629421/387846 今日发现服务器上多了一些名称为[sh] ...

  5. html制作细线表格

    关于这个细线表格的制作方法,百度一下可能就会有答案告诉你设置这几个值:给table设置border="0" cellspacing="1" bgcolor=&q ...

  6. BZOJ3165:[HEOI2013]Segment

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  7. RDA项目打包

    注意APP的编译搭建: ./aps/Makefile.toolchain //ccoption path的设定 ./aps/rules.mak //统一的编译规则 MAKE -C 1.TOOLS的可执 ...

  8. Oracle字段增删改方法总结

    一.修改字段的语法:alter table tablename modify (字段名 类型 [default value][null/not null],….);有一个表名为tb,字段段名为name ...

  9. Django admin有用的自定义功能

    引用园友 无名小妖 的博客 https://www.cnblogs.com/wumingxiaoyao/p/6928297.html 写的很好,但是博客园不能转载,不过我已经点赞了~

  10. 获取iOS应用中当前处于Activity状态的ViewController

    转自:http://blog.csdn.net/lixuwen521/article/details/41447507 // 获取当前处于activity状态的view controller - (U ...