Nearest Common Ancestors
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 23795 |
|
Accepted: 12386 |
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 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
2161 148 510 165 94 68 44 101 136 1510 116 710 216 38 116 1216 752 33 43 11 53 5
Sample Output
43
Source
The following template code is quoted from a great God's blog http://www.cppblog.com/menjitianya/archive/2015/12/10/212447.html
void Lca_tarjan (int u) {
make_set (U); //NOTE 1
ancestor[Find (u)] = u; //NOTE 2 for
(int i = 0; i < edge[u].size (); i++) { // Note 3
int v = edge[u][i].v;
Lca_tarjan (v); // Note 4
merge (U, v); // Note 5
ancestor[Find (u)] = u; // Note 6
}
Colors[u] = 1; // Note 7 for
(int i = 0; i < lcainfo[u].size (); i++) {
int v = lcainfo[u][i].v;
if (Colors[v]) { // Note 8
lcadataans[Lcainfo[u][i].idx].lca = ancestor[Find (v)];
}
}
}Note 1: Create a collection with only one element in the collection u, i.e. {u}NOTE 2: Since you have only one element in the collection, you can also write ancestor[u] = uNote 3:edge[u][0...size-1] stores the direct child nodes of UNote 4: Recursive calculation of all direct child nodes of U vNote 5: Collection merges when backtracking, merging the subtree with the root of V and the collection of UNote 6: Set the root node of the collection corresponding subtree for the merged collection, find (U) is the representative of the collectionnote 7:u for root subtree access complete, set node colorNote 8: Enumerate all and U related queries (U, v), if the subtree with the root of V has been visited, then Ancestor[find (v)] must have been calculated and must be a LCA of U and vFor loop for note 8, this problem only queries two points, no need to use another adjacency table
//Test Instructions: T set of test cases, enter N, then enter n-1 to form a tree, the nth group of data represents the nearest public ancestor to ask a B#include <stdio.h>#include<algorithm>#include<string.h>#include<vector>using namespacestd;Const intMAX =10005; Vector<int>Mp[max];intFather[max];intIndegree[max];///the 0-in is the root nodeint_rank[max];///using heuristic merging to prevent tree from forming chainintVis[max];intAnces[max];intb;voidInitintN) { for(intI=1; i<=n;i++) {Ances[i]=0; Vis[i]=0; _rank[i]=0; Indegree[i]=0; Father[i]=i; Mp[i].clear (); }}int_find (intx) { if(X==father[x])returnx; returnFATHER[X] =_find (father[x]);}void_union (intAintb) { intx =_find (a); inty =_find (b); FATHER[X]=y;} intTarjan (intu) { for(intI=0; I<mp[u].size (); i++) {Tarjan (mp[u][i]); _union (U,mp[u][i]); Ances[_find (u)]=u; } Vis[u]=1; if(A==u && vis[b]) printf ("%d\n", Ances[_find (B)]); Else if(B==u && Vis[a]) printf ("%d\n", Ances[_find (A)]); return 0; }intMain () {inttcase; scanf ("%d",&tcase); while(tcase--){ intN; scanf ("%d",&N); Init (n); intb; for(intI=1; i<n;i++) {scanf ("%d%d",&a,&b); Mp[a].push_back (b); INDEGREE[B]++; } scanf ("%d%d",&a,&B); for(intI=1; i<=n;i++){ if(indegree[i]==0) {Tarjan (i); Break; } } } return 0;}
POJ 1330 (Preliminary study on LCA)