poj 1330

Nearest Common Ancestors

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18598   Accepted: 9860

Description

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

Source

Taejon 2002

此题思路很多,并查集什么的,BFS等,我们主要是利用这些给出的边来利用合理的存储结构建树,以便利我们查找最近的公共父节点,于是我们想到将每个节点分层,并且记录记录每个节点其最近的父节点,然后通过DFS为每个节点维护一个层次信息,这样之后,当给出X,Y,我们只需要分析其层次高低,决定沿着谁的父节点走,直到x==y就找到了最近的公共父节点了。

 1 #include<iostream>
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxnum = 10000;
 7
 8 vector<int> Son[maxnum];  //存储每个节点的孩子
 9 int level[maxnum];  // 存储每个节点的层次
10 int parent[maxnum]; //存储每个节点的父亲节点
11
12
13 void DFS(int number,int deep)
14 {
15     level[number]=deep;
16     for(vector<int>::iterator it = Son[number].begin();it!=Son[number].end();it++)
17         DFS(*it,deep+1);
18 }
19
20 int main()
21 {
22     int t;
23     while(scanf("%d",&t)!=EOF)
24     {
25         while(t--)
26         {
27             int num;
28             scanf("%d",&num);
29             int dad,son;
30             for(int i=0;i<maxnum;i++)
31                 Son[i].clear();
32             memset(parent,-1,sizeof(parent));
33             for(int i=0;i<num-1;i++)
34             {
35                 scanf("%d%d",&dad,&son);
36                 Son[dad-1].push_back(son-1);
37                 parent[son-1] = dad-1;   //存储每个节点的上一个节点
38             }
39             int t=dad-1;
40             while(parent[t]!=-1)
41                 t=parent[t];
42             DFS(t,0);   //遍历分层
43
44             int x,y;
45             scanf("%d%d",&x,&y);
46             x=x-1;
47             y=y-1;
48             while(x!=y)
49             {
50                 if(level[x]<level[y])
51                     y=parent[y];
52                 else
53                     x = parent[x];
54             }
55             printf("%d\n",x+1);
56
57
58         }
59     }
60     return 0;
61 }
时间: 2024-10-12 13:01:11

poj 1330的相关文章

POJ 1330 Nearest Common Ancestors 倍增算法的LCA

POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节点的第2j个父亲是多少   这个代码不是我的,转自 邝斌博客 1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-9-5 9:45:17 4 File Name :F

LCA 算法学习 (最近公共祖先)poj 1330

poj1330 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,处理技巧就是在回溯到结点u的时候,u的子树已经遍历,这时候才把u结点放入合并集合中,这样u结点和所有u的子树中的结点的最近公共祖先就是u了,u和还未遍历的所有u的兄弟结点及子树中的最近公共祖先就是u的父亲结点.这样我们在对树深度遍历的时候就很自然的将树中的结点分成若干的集合,两个集合中的所属不同集合的任意一对顶点的公共祖先都是相同的,也就是说这两个集合的最近公共祖先只有一个.时间复杂度为O(n+q

POJ 1330 Nearest Common Ancestors(树)

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17628   Accepted: 9335 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

POJ 1330 Nearest Common Ancestors LCA题解

本题是一个多叉树,然后求两点的最近公共单亲节点. 就是典型的LCA问题.这是一个很多解法的,而且被研究的很透彻的问题. 原始的解法:从根节点往下搜索,若果搜索到两个节点分别在一个节点的两边,那么这个点就是最近公共单亲节点了. Trajan离线算法:首次找到两个节点的时候,如果记录了他们的最低单亲节点,那么答案就是这个最低的单亲节点了. 问题是如何有效记录这个最低单亲节点,并有效根据遍历的情况更新,这就是利用Union Find(并查集)记录已经找到的节点,并及时更新最新访问的节点的当前最低单亲节

POJ 1330 Nearest Common Ancestors (在线LCA转RMQ)

题目地址:POJ 1330 在线LCA转RMQ第一发.所谓在线LCA,就是先DFS一次,求出遍历路径和各个点深度,那么求最近公共祖先的时候就可以转化成求从u到v经过的点中深度最小的那个. 纯模板题. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h&g

POJ 1330 裸的LCA

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17720   Accepted: 9393 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:  In the figure, each

POJ 1330 Nearest Common Ancestors LCA(在线RMQ,离线Tarjan)

链接:http://poj.org/problem?id=1330 题意:只看题目就知道题目是什么意思了,最近公共祖先,求在一棵树上两个节点的最近公共祖先. 思路:求最近公共祖先有两种算法,在线和离线,在线方法是用RMQ求LCA,一句话总结就是在从DFS时,从第一个点到第二个点的最短路径中深度最浅的点就是公共祖先,用RMQ处理,一般问题的最优解决方式的复杂度是O(NlogN)的预处理+N*O(1)的查询.离线方法是Tarjan算法,将所有询问的两个点都记录下来,在DFS过程中不断将每个点自身作为

Nearest Common Ancestors POJ - 1330

Nearest Common Ancestors POJ - 1330 题意:找两个点公共祖先,裸题. 1 #include <cstdio> 2 #include <cstring> 3 const int maxn=10010; 4 5 int f[maxn],vis[maxn]; 6 7 int main(){ 8 int n,t; 9 // freopen("in.txt","r",stdin); 10 scanf("%d&

[POJ 1330] Nearest Common Ancestors (朴素方法)

POJ 1330: Nearest Common Ancestors Time Limit: 1000ms Memory Limit: 32Mb Description 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 fro

POJ 1330 LCA裸题~

POJ 1330 Description 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 anc