POJ1330(LCA入门题)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 23388 | Accepted: 12195 |
Description

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.
Write a program that finds the nearest common ancestor of two distinct nodes in a tree.
Input
Output
Sample Input
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
struct Edge{
int to,next;
}es[MAXN];
int V,E;
int head[MAXN];
int root;
void add_edge(int u,int v)
{
es[E].to=v;
es[E].next=head[u];
head[u]=E++;
}
int par[MAXN];
void prep()
{
for(int i=;i<MAXN;i++)
{
par[i]=i;
}
}
int fnd(int x)
{
if(par[x]==x)
{
return x;
}
return par[x]=fnd(par[x]);
}
void unite(int x,int y)
{
par[y]=fnd(x);
}
int dep[MAXN];
int fa[MAXN];
void dfs(int u,int f,int d)
{
dep[u]=d;
fa[u]=f;
for(int i=head[u];i!=-;i=es[i].next)
{
int to=es[i].to;
if(f!=to) dfs(to,u,d+);
}
}
int LCA(int u,int v)
{
while(dep[u]>dep[v]) u=fa[u];
while(dep[v]>dep[u]) v=fa[v];
while(u!=v)
{
u=fa[u];
v=fa[v];
}
return u;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
prep();
memset(head,-,sizeof(head));
scanf("%d",&V);
E=;
for(int i=;i<=V-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
unite(u,v);
}
root=fnd();
dfs(root,-,);
int x,y;
scanf("%d%d",&x,&y);
int lca=LCA(x,y);
printf("%d\n",lca);
}
return ;
}
POJ1330(LCA入门题)的更多相关文章
- LCA入门题集小结
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目: How far away ? Time Limit: 2000/1000 MS (Jav ...
- poj 1330(RMQ&LCA入门题)
传送门:Problem 1330 https://www.cnblogs.com/violet-acmer/p/9686774.html 参考资料: http://dongxicheng.org/st ...
- lca入门———树上倍增法(博文内含例题)
倍增求LCA: father[i][j]表示节点i往上跳2^j次后的节点 可以转移为 father[i][j]=father[father[i][j-1]][j-1] 整体思路: 先比较两个点的深度, ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1754:I Hate It(线段树,入门题,RMQ问题)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
随机推荐
- 畅通project再续 HDU杭电1875 【Kruscal算法 || Prim】
Problem Description 相信大家都听说一个"百岛湖"的地方吧.百岛湖的居民生活在不同的小岛中.当他们想去其它的小岛时都要通过划小船来实现.如今政府决定大力发展百岛湖 ...
- javascript调试常用工具讲解
.Console命令详解,让调试js代码变得更简单 2.<Firebug入门指南>
- SSH 使用密钥登录并禁止口令登录
小结:修改下sshd配置文件,把公钥传上去就好了 先生成公钥和私钥,默认在/root/.ssh/目录,可以先看一下有没有这个目录. 生成公钥后,以后其它服务器也都可以复用这个公钥 最好生成时输入密码! ...
- iOS中数组遍历的方法及比較
数组遍历是编码中非经常见的一种需求.我们来扒一拔iOS里面都有什么样的方法来实现,有什么特点. 由于iOS是兼容C语言的.所以C语言里面的最最常见的for循环遍历是没有问题的. 本文中用的数组是获取的 ...
- 利用.dSYM跟.app文件准确定位Crash位置
本文转载至 http://blog.csdn.net/lvxiangan/article/details/28102629 利用.dSYM和.app文件准确定位Crash位置首先,确保 ...
- python 基础 1.5 数据类型(二)--列表
一.python 数据类型序列---列表 1.列表是可变型的数据类型.列表里边的元素是可变的,可以增加,可以删除. 2.列表(list)是处理一组有序项目的数据结构,即可以在列表中存储一个序列的项 ...
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- The goroutine scheduler is not preemptive.
go - Why is time.sleep required to run certain goroutines? - Stack Overflow https://stackoverflow.co ...
- Grunt实战 --- 通过nodejs和Grunt实现项目在线构建
本文主要说明,实现在线自动构建项目的实现方法.
- centos7 安装jdk9 总结
升级jdk, 从jdk8 升级到jdk9 1:卸载jdk8: 1〉 [root@localhost conf.d]# rpm -qa|grep java javapackages-tools-3.4. ...