洛谷 SP14932 LCA - Lowest Common Ancestor

洛谷评测传送门

题目描述

A tree is an undirected graph in which any two vertices are connected by exactly one simple path. In other words, any connected graph without cycles is a tree. - Wikipedia

The lowest common ancestor (LCA) is a concept in graph theory and computer science. Let T be a rooted tree with N nodes. The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself). - Wikipedia

Your task in this problem is to find the LCA of any two given nodes v and w in a given tree T.

For example the LCA of nodes 9 and 12 in this tree is the node number 3.

Input

The first line of input will be the number of test cases. Each test case will start with a number N the number of nodes in the tree, 1 <= N <= 1,000. Nodes are numbered from 1 to N. The next N lines each one will start with a number M the number of child nodes of the Nth node, 0 <= M <= 999 followed by M numbers the child nodes of the Nth node. The next line will be a number Q the number of queries you have to answer for the given tree T, 1 <= Q <= 1000. The next Q lines each one will have two number v and w in which you have to find the LCA of v and w in T, 1 <= v, w <= 1,000.

Input will guarantee that there is only one root and no cycles.

Output

For each test case print Q + 1 lines, The first line will have “Case C:” without quotes where C is the case number starting with 1. The next Q lines should be the LCA of the given v and w respectively.

Example

Input:
1
7
3 2 3 4
0
3 5 6 7
0
0
0
0
2
5 7
2 7 Output:
Case 1:
3
1

输入格式

输出格式

题意翻译

Description:

一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树。-Wikipedia

最近公共祖先(LCA)是……(此处省去对LCA的描述),你的任务是对一棵给定的树TT以及上面的两个节点u,vu,v求出他们的LCALCA

例如图中99和1212号节点的LCA*L*C*A*为33号节点

Input:

输入的第一行为数据组数TT,对于每组数据,第一行为一个整数N(1\leq N\leq1000)N(1≤N≤1000),节点编号从11到NN,接下来的NN行里每一行开头有一个数字M(0\leq M\leq999)M(0≤M≤999),MM为第ii个节点的子节点数量,接下来有MM个数表示第ii个节点的子节点编号。下面一行会有一个整数Q(1\leq Q\leq1000)Q(1≤Q≤1000),接下来的QQ行每行有两个数u,vu,v,输出节点u,vu,v在给定树中的LCALCA

输入数据保证只有一个根节点并且没有环。

Output:

对于每一组数据输出Q+1Q+1行,第一行格式为"Case i:"(没有双引号),i表示当前数据是第几组,接下来的QQ行每一行一个整数表示一对节点u,vu,v的LCALCA

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_gl

输入输出样例

题解:

LCA模板题目双倍经验~~

点进来的小伙伴肯定还不太会LCA...

请参考蒟蒻的这篇博客:

(这里介绍了倍增求LCA,其实求LCA还有好多方式,比如离线Tarjan和树链剖分等,有兴趣的巨佬可以自己涉及,如果只求LCA的话,还是这种倍增法更快一些)

求解LCA问题的几种方式

当然,本题还有一些小细节,比如多组数据数据要清空,以及比较奇葩的读入边的方式。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1010;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48){if(ch=='-')f=-1;ch=nc();}
while(ch>47) x=(((x<<2)+x)<<1)+ch-48,ch=nc();
return x*f;
}
int n,m,q;
int tot,head[maxn],nxt[maxn<<1],to[maxn<<1];
int deep[maxn],fa[maxn][21];
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x,int f)
{
deep[x]=deep[f]+1;
fa[x][0]=f;
for(int i=1;(1<<i)<=deep[x];i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(y==f)
continue;
dfs(y,x);
}
}
int lca(int x,int y)
{
int ret;
if(deep[x]<deep[y])
swap(x,y);
for(int i=20;i>=0;i--)
if(deep[fa[x][i]]>=deep[y])
x=fa[x][i];
if(x==y)
return y;
for(int i=20;i>=0;i--)
{
if(fa[x][i]!=fa[y][i])
{
x=fa[x][i];
y=fa[y][i];
}
else
ret=fa[x][i];
}
return ret;
}
int main()
{
int t;
t=read();
for(int k=1;k<=t;k++)
{
tot=0;
memset(head,0,sizeof(head));
memset(nxt,0,sizeof(nxt));
memset(to,0,sizeof(to));
memset(deep,0,sizeof(deep));
memset(fa,0,sizeof(fa));
n=read();
for(int i=1;i<=n;i++)
{
m=read();
if(!m)
continue;
for(int j=1;j<=m;j++)
{
int u=read();
add(u,i);
add(i,u);
}
}
dfs(1,0);
q=read();
printf("Case %d:\n",k);
while(q--)
{
int u=read();
int v=read();
printf("%d\n",lca(u,v));
}
}
return 0;
}

洛谷 SP14932 LCA - Lowest Common Ancestor的更多相关文章

  1. SP14932 LCA - Lowest Common Ancestor

    Description: 一棵树是一个简单无向图,图中任意两个节点仅被一条边连接,所有连通无环无向图都是一棵树.\(-Wikipedia\) 最近公共祖先(\(LCA\))是--(此处省去对\(LCA ...

  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. 当Python遇上AI(一)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.什么是学习 如果要给学习下一个定义,那么根据某百科给出的定义是——学习,是指通过阅读.听讲.思考和实践等途径获 ...

  2. JT/T 808-2013 道路运输车辆卫星定位系统北斗兼容车载终端通讯协议技术规范

    文档下载地址:JT/T 808-2013 道路运输车辆卫星定位系统北斗兼容车载终端通讯协议技术规范

  3. 从0使用Ruby on Rails打造企业级RESTful API项目实战之我的云音乐

    本节对我们项目实现的功能和知识点做一个简单的介绍,因为是RESTful API项目,所以对于后端来说基本上没有什么UI界面可展示,那我们就在关键的点,使用客户端(Android)实现的效果图. 课程简 ...

  4. 白话 MVC、MVP、MVVP

    白话 MVC.MVP.MVVP 注意这里单纯的通过例子来讲解 MVC MVP MVVP 这三种架构模式的起源和作用,不牵扯某种特定的语言.具体到各种语言各种软件系统上体现有所不同,但是原理都是这样的. ...

  5. 用iText5-1-生成PDF

    参考代码和图片出处 https://howtodoinjava.com/library/read-generate-pdf-java-itext/ pom引入jar包 <dependencies ...

  6. springioc之依赖注入

    1.1.2  IoC能做什么 IoC不是一种技术,只是一种思想,一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合.更优良的程序.传统应用程序都是由我们在类内部主动创建依赖对象,从而导致类与类 ...

  7. jxl.jar下载

    jxl.jar是通过java操作excel表格的工具类库,是由java语言开发而成的. 在网上找了很多,不是链接失效,就是csdn上要钱的,所以干脆上传个到自己的博客文件里,方便你们下载. 下载地址: ...

  8. 关于eclipse创建的[传统web项目][传统maven项目][maven-web项目][springboot项目]目录结构

    总体比较 [传统web项目] [传统maven项目] [maven-web项目] [springboot项目] 本文摘至https://blog.csdn.net/qq_42747738/articl ...

  9. Vue父子组件数据双向绑定,子组件可修改props

    第一种,子组件通过监听父组件数据,子组件改变数据之后通知给父组件 原文链接:https://blog.csdn.net/m0_37728716/article/details/81776929 父组件 ...

  10. Springboot整合Mybatis实现级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...