Nearest Common AncestorsTime limit:1000msMemory limit:10000kTotal submissions:19636accepted:10412Description
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 a labeled with a integer from {1, 2,..., 16}. Node 8 is the root of the tree. Node x is a ancestor of node y if node x is in the path between the root and node Y. For example, node 4 was an ancestor of node 16. Node also an ancestor of node 16. As a matter of fact, nodes 8, 4, 16, and + are the ancestors of the node. Remember that a node was 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 the 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 and 7. A node x is called the nearest common ancestor of nodes Y and Z if x are a common ancestor of Y and Z and nearest to Y and Z among their common ancestors. Hence, the nearest common ancestor of nodes and 7 is node 4. Node 4 is nearer-nodes and 7 than node 8 is.
For other examples, the nearest common ancestor of nodes 2 and 3 are node, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and are node 4. The last example, if Y is a ancestor of Z, then the nearest common ancestor of Y and Z are y.
Write A program This finds the nearest common ancestor of the distinct nodes in a tree.
Input
The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case is starts with a line containing an integer N and the number of nodes in a tree, 2<=n<=10,000. The nodes is labeled with integers 1, 2,..., N. Each of the next N-1 lines contains a pair of integers this represent an edge--the first integer is the parent node of T He second integer. Note that a tree with N nodes have exactly N-1 edges. The last line of all test case contains, distinct integers whose nearest common ancestor are to be computed.
Output
Print exactly one line for each test case. The line should contain the "the integer" 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
The main topic: give you a tree, there are N nodes, N-1 edge. Finally ask the nearest two points (u,v) from the tree
What is the public ancestor?
For example: The recent public ancestor of 6 and 16 is 4;14 and 1 of the nearest public ancestor is 1.
Idea: For the recent public ancestor LCA problem, the most classic off-line algorithm is the TARJAN-LCA algorithm. Use chain forward
The star storage graph and the query, head[] and edges[] indicate that the graph (tree), qhead[] and qedges[] are queried. Collection
The operation is implemented using the and check set. This problem uses the indegree[] array to store the node's penetration, finding the degree of 0
Root node, called LCA (Root).
TARJAN-LCA algorithm:
For each point u:
1. Create a set of elements that are represented by U.
2. Traverse the node v connected to U, if not accessed, for V using the TARJAN-LCA algorithm, after the end, the
The set of V is incorporated into the set of U.
3. For queries related to node U (u,v), if V is accessed, the result is the element represented by the set of V
Of
Reference: ACM-ICPC Programming Series--Graph theory and application
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 20020;struct edgenode{int to; int LCA; int next;}; Edgenode edges[maxn],qedges[maxn];int father[maxn],head[maxn],qhead[maxn],indegree[maxn];int Find (int x) {if (x! = FATHER[X]) Father[x] = find (father[x]); return father[x];} BOOL Vis[maxn];void LCA (int u) {father[u] = u; Vis[u] = true; for (int k = Head[u]; K! =-1; k = edges[k].next) {if (!vis[edges[k].to]) {LCA (edges[k].to); Father[edges[k].to] = u; }} for (int k = Qhead[u]; K! =-1; k = qedges[k].next) {if (vis[qedges[k].to]) {qedges[ K].lca = Find (qedges[k].to); Qedges[k^1].lca = Qedges[k].lca; }}}int Main () {int t,n,u,v; scanf ("%d", &t); while (t--) {memset (indegree,0,sizeof (Indegree)); memset (father,0,sizeof (father)); memset (head,-1,sizeOf (Head)); memset (qhead,-1,sizeof (Qhead)); memset (vis,false,sizeof (VIS)); memset (edges,0,sizeof (Edges)); memset (qedges,0,sizeof (qedges)); scanf ("%d", &n); int id = 0; for (int i = 0; i < N-1; ++i) {scanf ("%d%d", &u,&v); indegree[v]++; Edges[id].to = v; Edges[id].next = Head[u]; Head[u] = id++; edges[id].to = u; Edges[id].next = Head[v]; HEAD[V] = id++; } int iq = 0; scanf ("%d%d", &u,&v); Qedges[iq].to = v; Qedges[iq].next = Qhead[u]; Qhead[u] = iq++; qedges[iq].to = u; Qedges[iq].next = Qhead[v]; QHEAD[V] = iq++; int root; for (int i = 1; I <= N; ++i) {if (!indegree[i]) {root = i; Break }} LCA (root); printf ("%d\n", QEDGES[0].LCA); } return 0;}
POJ1330 Nearest Common Ancestors "recent public ancestor" "Tarjan-lca algorithm"