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
Test instructions
is the nearest public ancestor seeking two points.
Analysis
This problem can be multiplied to do, this time I used a tree profile, it feels pretty good to play, um ...
It feels like it's impossible to jump. Two heavy chains cross the LCA because a point is only a heavy edge down. So it's good to skip the larger side of the DEP every time.
int LCA (int a, int b) {while (1) { if (Top[a]==top[b]) return dep[a]<=dep[b]?a:b; else if (Dep[top[a]]>=dep[top[b]]) a=fa[top[a]; else b=fa[top[b]];} }
That's it.
The feeling is because it is impossible to jump two sides are over the LCA, because a point is attached to a heavy edge, so we choose the short one each time the jump is good.
Http://www.xuebuyuan.com/552070.html
Here is a detailed proof ^ ^^
The code is as follows:
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 #defineMAXN 100108 #defineINF 1000000009 Ten intFA[MAXN],FIRST[MAXN],SIZE[MAXN],DEP[MAXN],SON[MAXN]; One intW[MAXN],TOP[MAXN];intWL; A BOOLQ[MAXN]; - - structnode the { - intX,y,next; -}t[2*MAXN];intLen; - + intMymax (intXintY) {returnX>y?x:y;} - intMymin (intXintY) {returnX<y?x:y;} + A voidInsintXinty) at { -t[++len].x=x;t[len].y=y; -t[len].next=first[x];first[x]=Len; - } - - voidDFS1 (intXintf) in { -fa[x]=f;dep[x]=dep[f]+1; size[x]=1; toson[x]=0; + for(intI=first[x];i;i=t[i].next)if(t[i].y!=f) - { the DFS1 (t[i].y,x); *size[x]+=SIZE[T[I].Y]; $ if(Size[t[i].y]>size[son[x]]) son[x]=t[i].y;Panax Notoginseng } - } the + voidDFS2 (intXintTP) A { thew[x]=++WL; +top[x]=TP; - if(size[x]!=1) DFS2 (SON[X],TP); $ for(intI=first[x];i;i=t[i].next)if(t[i].y!=fa[x]&&t[i].y!=Son[x]) $ { - DFS2 (T[I].Y,T[I].Y); - } the } - Wuyi intLCA (intAintb) the { - while(1) Wu { - if(Top[a]==top[b])returnDEP[A]<=DEP[B]?a:b; About Else if(Dep[top[a]]>=dep[top[b]]) a=Fa[top[a]]; $ Elseb=Fa[top[b]]; - } - } - A + the intMain () - { $ intT; thescanf"%d",&T); the while(t--) the { the intN; -scanf"%d",&n); inmemset (First,0,sizeof(first)); thememset (Q,0,sizeof(q)); thelen=0; About for(intI=1; i<n;i++) the { the intx,y,c; thescanf"%d%d",&x,&y); +q[y]=1; -Ins (x, y);//ins (y,x); the }Bayi intRoot; the for(intI=1; i<=n;i++)if(!q[i]) root=i; thedep[0]=0; size[0]=0; -DFS1 (Root,0); wl=0; -DFS2 (Root,1); the intx, y; thescanf"%d%d",&x,&y); theprintf"%d\n", LCA (x, y)); the } - return 0; the}
[POJ1330]
2016-05-08 17:13:07
"POJ1330" Nearest Common ancestors (tree chain for LCA)