# Nearest Common Ancestors

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.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
#include <cctype>
#include <cfloat>
#include <climits>
#include <complex>
#include <deque>
#include <list>
#include <set>
#include <utility>
#define rt return
#define fr freopen("in.txt","r",stdin)
#define fw freopen("out.txt","w",stdout)
#define ll long long
#define ull unsigned long long
#define detie ios_base::sync_with_stdio(false);cin.tie(false);cout.tie(false)
#define pii pair<int,int>
#define lowbit(x) x&(-x)
using namespace std;
#define maxi 0x3f3f3f3f
#define MAX 100010

map<int, int> m;
vector<int> va, vb;
int main()
{
//fr;
detie;
int T;
cin >> T;
while (T--)
{
m.clear();
va.clear();
vb.clear();
int n, a, b;
cin >> n;
for (int i = 0; i < n - 1; i++)
{
cin >> a >> b;
m[b] = a;
}
cin >> a >> b;
while (m.count(a))
{
va.push_back(a);
a = m[a];
}
va.push_back(a);
while (m.count(b))
{
vb.push_back(b);
b = m[b];
}
vb.push_back(b);
bool flag = false;
int ans;
for (int i = 0; i < va.size() && !flag; i++)
{
for (int j = 0; j < vb.size() && !flag;j++)
{
if (va[i] == vb[j])
{
ans = va[i];
flag = true;
}
}
}
printf("%d\n", ans);
}
rt 0;
}

• 本文已收录于以下专栏：

## POJ1330 Nearest Common Ancestors(最近公共祖先LCA 并查集+DFS)

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

## poj 1330 Nearest Common Ancestors（简单的树初探）

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submiss...

## poj1330 Nearest Common Ancestors（LCA离线算法）

http://poj.org/problem?id=1330 题意：给你n个节点和n-1个关系，通过关系建起一棵树，然后给你两个节点，求他们的最小公共祖先。 ps：这里的Tarjan离线算法貌似...

## LCA-倍增思想 POJ1330 Nearest Common Ancestors

LCA 倍增
• ivy_uu
• 2016年07月07日 23:11
• 181

## poj 1330 Nearest Common Ancestors(LCA模板)

http://poj.org/problem?id=1330 题意：给出两个点，求出这两个点最近的公共祖先。 求LCA的模板题。 大致思路就是访问到某个节点时，先将它自己加入集合中，然后递归访问它的子...

## poj 1330 Nearest Common Ancestors 朴素LCA算法

Nearest Common AncestorsTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9213 Accepted: 488...

## A - Nearest Common Ancestors（8.1.1）

Description A rooted tree is a well-known data structure in computer science and engineering. An ...

## 【LCA】 POJ 1330 Nearest Common Ancestors

LCA模板题。。用一下tarjan的离线suan

## poj 1330 Nearest Common Ancestors（最近公共祖先（LCA））

Description A rooted tree is a well-known data structure in computer science and engineering. An ex...

举报原因： 您举报文章：Nearest Common Ancestors 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)