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. css水平居中,竖直居中技巧(二)

    css水平居中,竖直居中技巧(二)===### 1.效果 ### 2.代码#### 2.1.index.html <!DOCTYPE html> <html lang="z ...

  2. Unity 之 Shader 面的剔除 Cull

    面的剔除 Cull 在渲染的时候,默认情况下是只有朝向摄像机的面才会被渲染,可以告诉Unity,我想渲染哪一个朝向的面,使用Cull命令在计算体积阴影的时候会用到对Cull的操作来计算和物体相交的投影 ...

  3. Opencv 图像矩

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  4. 1.spark的wordcount解析

    一.Eclipse(scala IDE)开发local和cluster (一). 配置开发环境 要在本地安装好java和scala.  由于spark1.6需要scala 2.10.X版本的.推荐 2 ...

  5. radio后的input框数据传递

    <input type="radio" name="limit_type" value="total">活动期间,每个手机号可抽 ...

  6. [原创]COCOS2DX 3.8 增加protobuf为external库

    此处为记录防止忘记,最近使用COCOS练习一些新的库,所以需要把PROTOBUF编译进去,看上去很麻烦,这里记录一下,以防忘记. 首先下载protobuf 2.5.0版本,下载地址请自行百度. 准备工 ...

  7. wins 软件安装

    1.x86 x64区别86就是原来的32位操作系统64就是现在比较新的64位操作系统

  8. 实践作业4---DAY3阶段二。

    第二阶段是用户调研,我们小组选择了一个5班的同学,作为采访对象.采访比较详实,但是样本太少,不太有说服力. 具体采访详情如下: 问:请问您使用喜欢发表Blog么? 答:肯定有啊. 问:都用什么网站发表 ...

  9. Kubernetes (1.6) 中的存储类及其动态供给

    原文地址:http://blog.fleeto.us/translation/dynamic-provisioning-and-storage-classes-kubernetes-0?utm_sou ...

  10. 金士顿DT100 G3 PS2251-07海力士U盘量产修复成功教程

    金士顿DT100 G3这款U盘.用了大概一年半没什么问题,前段时间拷贝大文件,无奈电脑突然断电,然后这个U盘在电脑上就读不出来了,只能显示盘符,所以有了修复过程.   最开始我也是菜鸟,不知道怎么搞, ...