Nearest Common Ancestors
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 20983 |
|
Accepted: 11017 |
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
In solving the problem of the recent public ancestor, the Tarjan thought is used to form a deep search tree from the root node. The processing technique is when you go back to the node U. The subtree of u has been traversed. This is when you put the U-node into the merge set so that the recent public ancestor of the nodes in the U-node and all of the subtree is U, and you and the recent public ancestor of all U's sibling nodes and subtrees that you have not traversed are the father nodes of U.
in this way we are naturally dividing the nodes in the tree into a number of sets when we traverse the depth of the tree, and the common ancestor of the random pair of vertices belonging to the different sets in the two sets is the same, that is to say, the recent public ancestor of the two sets has only one. the time complexity is O (n+q), n is the node, q is the logarithm of the query node.
#include "stdio.h" #include "string.h" #include "vector" using namespace std; #define N 11000const int inf=1<<20; Vector<int>g[n];int s,t,n;int f[n],pre[n],ans[n];bool vis[n];int findset (int x) {if (x!=f[x]) F[x]=findset ( F[X]); return f[x];} int unionset (int a,int b) {int x=findset (a); int Y=findset (b); if (x==y) return x; F[y]=x; return x;} void LCA (int u) {int i,v; Ans[u]=u; For (I=0;i<g[u].size (); i++) {v=g[u][i]; Access to the individual child nodes derived from the parent node LCA (v); int X=unionset (U,V); Merging parent-child nodes into Ans[x]=u; The ancestor node is recorded as u} vis[u]=1; if (u==s&&vis[t])//Two nodes are visited in turn, the second interview satisfies the condition {printf ("%d\n", Ans[findset (t)]); return; } else if (U==t&&vis[s]) {printf ("%d\n", Ans[findset (s)]); return; }}int Main () {int i,u,v,t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (i=1;i<=n;i++) {pre[i]=-1; Record the parent node of node I f[i]=i; and the root node of vis[i]=0 is recorded. G[i].clear (); } for (i=1;i<n;i++) {scanf ("%d%d", &u,&v); G[u].push_back (v); Pre[v]=u; } scanf ("%d%d", &s,&t); for (i=1;i<=n;i++) if (pre[i]==-1) break; LCA (i); } return 0;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
LCA Learning Algorithm (nearest common ancestor) POJ 1330