POJ 1330 Nearest Common Ancestors

A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.

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

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

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

一道关于LCA的算法题,太弱的我不会做。贴出大神的AC代码,仅供参考学习

#include<iostream>
#include<vector>
#define MAX 10010
using namespace std; int n,flag;
int f[MAX],r[MAX],ancestor[MAX];
int indegreen[MAX],vis[MAX];
vector<int> head[MAX],Que[MAX]; void Init()
{
int i,a,b;
cin>>n;
flag=0;
for(i=1;i<=n;i++)
{
head[i].clear();
Que[i].clear();
f[i]=i;
r[i]=1;
ancestor[i]=0;
indegreen[i]=0;
vis[i]=0;
}
for(i=1;i<n;i++)
{
cin>>a>>b;
head[a].push_back(b);
indegreen[b]++;
}
cin>>a>>b;
Que[a].push_back(b);
Que[b].push_back(a);
} int Find(int u)
{
if(f[u]==u)
return f[u];
else
f[u]=Find(f[u]);
return f[u];
} void Union(int v,int u)
{
int a,b;
a=Find(v);
b=Find(u);
if(a==b)
return ;
if(r[a]<=r[b])
{
f[a]=b;
r[b]+=r[a];
}
else
{
f[b]=a;
r[a]+=r[b];
}
} void LCA(int k)
{
int i,size;
size=head[k].size();
ancestor[k]=k;
for(i=0;i<size;i++)
{
if(flag)
break;
LCA(head[k][i]);
Union(k,head[k][i]);
ancestor[Find(k)]=k;
}
vis[k]=1;
size=Que[k].size();
for(i=0;i<size;i++)
{
if(vis[Que[k][i]])
{
flag=1;
cout<<ancestor[Find(Que[k][i])]<<endl;
return ;
}
}
} int main()
{
int T;
cin>>T;
while(T--)
{
Init();
for(int i=1;i<=n;i++)
{
if(!indegreen[i])
{
LCA(i);
break;
}
}
}
return 0;
}

  

POJ 1330 Nearest Common Ancestors(lca)的更多相关文章

  1. poj 1330 Nearest Common Ancestors(LCA 基于二分搜索+st&rmq的LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30147   Accept ...

  2. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  3. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  4. poj 1330 Nearest Common Ancestors(LCA:最近公共祖先)

    多校第七场考了一道lca,那么就挑一道水题学习一下吧= = 最简单暴力的方法:建好树后,输入询问的点u,v,先把u全部的祖先标记掉,然后沿着v->rt(根)的顺序检查,第一个被u标记的点即为u, ...

  5. POJ 1330 Nearest Common Ancestors(LCA Tarjan算法)

    题目链接:http://poj.org/problem?id=1330 题意:给定一个n个节点的有根树,以及树中的两个节点u,v,求u,v的最近公共祖先. 数据范围:n [2, 10000] 思路:从 ...

  6. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  7. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  8. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  9. POJ 1330 Nearest Common Ancestors(裸LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39596   Accept ...

随机推荐

  1. Ubuntu 安装 kamailio

    首先安装前,你已经对kamailio的基本用法了解.可根据情况选择安装方式,本次安装基于Ubuntu18.04系统安装,对于16.04及一下会遇到版本问题,请自己查阅文档解决 安装第三方库 sudo ...

  2. linux安装wifi驱动,开热点

    本次安装的debian系统安装的时候提示wifi硬件需要安装非自由固件才能运行,并告诉本硬件要安装的固件名字叫做iwlwifi-2030-6.ucode.是iwlwifi驱动适配我的wireless硬 ...

  3. S数据导入

    一. 职责下的视图导入先对比UAT172和PROD 177的数据,若177比172多的数据进行删除,若172比177多的数据,重新加入到177. 1.删除177多余的 搜索一个177比172多的,进行 ...

  4. Apache Hive (三)Hive元数据信息对应MySQL数据库表

    转自:https://www.cnblogs.com/qingyunzong/p/8710356.html 概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理. ...

  5. linux: 空指令(:)

    :指令 描述: 空命令,除了参数替换和重定向外不执行任何操作,总是保证退出码为0. eg1:创建文件(不需要调用其它程序,速度更快) :>/path/to/file 测试: 创建10000个不存 ...

  6. c++之带默认形参值的函数

    先来个例子: #include <iostream> using namespace std; ,){ return x+y; } int main(){ //freopen(" ...

  7. th:onclik()传参问题(前端使用了bootstrap)

    网上大多帖子是这么写的 onclick调javascript函数时,不能直接使用onclick=“editUser(${prod.id})”,这样会报错,需要修改成如下的格式. <a href= ...

  8. IDEA 提示找不到 javax 等 tomcat 的相关包

    网上很多方法都告诉你,把 javax 的 libs 拷贝到项目下吧,简直简单粗暴.其实有更好的办法. 1.首先进入 Run 其中的 Run/Debug Configurations,在 Server ...

  9. UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释(转)

    layoutSubviews总结 ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size- (void)sizeToFit—————— - (void ...

  10. spring源码学习——spring整体架构和设计理念

    Spring是在Rod Johnson的<Expert One-On-One J2EE Development and Design >的基础上衍生而来的.主要目的是通过使用基本的java ...