Nearest Common Ancestors

原创 2016年08月30日 21:34:42
链接:http://poj.org/problem?id=1330

题目: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.

题意:给一棵树,求任意两点的最小公共祖先。

分析:一开始想了半天,以为是个建树的题目,一项就感觉可能很恶心。后来看了看别人的。。。。直接暴力去搜一下就过了好么。。。。。

题解:
#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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1330 Nearest Common Ancestors(求LCA的三种方法)

1.离线Tarjan        设我们求点对(u,v)的最近公共祖先。        利用在DFS过程中,从点u第一次到点v的过程中,必定是从u开始,经过u和v的最近公共祖先的S,然后到达...

ZOJ 3626 Treasure Hunt I(树型DP+01背包)

题意:给一棵有n个结点的树,每个点有点权表示在这个点上的价值,每条边有边权表示走这条路所需要的时候,给一个时间m,问在时间m从点k出发再回到点k所能得到的最大的价值和。 #include #inc...

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

题目链接:..G20过了再补 题目大意:求最近公共祖先...水.. 题解:倍增LCA/树剖 就是练模版的题...话说看了一下,我的树剖比倍增还快了那么一丢丢.. 注意:不是无根树啊23...

poj1330 Nearest Common Ancestors(LCA离线算法)

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

LCA-倍增思想 POJ1330 Nearest Common Ancestors

LCA 倍增

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 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)