Nearest Common Ancestors

链接: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值