Title Link: http://poj.org/problem?id=1330
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 an D 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.
Test instructions Description: In a DAG, the definition node U is the ancestor of Node V: node U is a node on the path of the tree root to Node v. The relationship between some nodes is given, and the nearest common ancestor of two nodes is obtained.
Algorithmic Analysis: The entry question of the recent public ancestor (LCA).
General idea of the recent common ancestor algorithm:
1: Find the ancestor node of 2^k (0<=k<max_log_n) for each node. Node U's 2^0 (first generation) ancestor node is the Father node of U, then we can get U's first generation, second generation, fourth generation, eighth generation ... Ancestor nodes.
2: the node U and v depth of the node according to the algorithm in the 1 of the idea moved to the same depth of the node with a small depth (root depth of 0, the son of the root of the node depth of 1), and then move up together, you can find the LCA.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8 #defineINF 0x7fffffff9 using namespacestd;Ten Const intmaxn=10000+Ten; One Const intmax_log_maxn= -; A - intN,a,b,root; -vector<int>G[MAXN]; the intFATHER[MAX_LOG_MAXN][MAXN],D[MAXN]; - - voidDfsintUintPintdepth) - { +father[0][u]=p; -d[u]=depth; + intnum=g[u].size (); A for(intI=0; i<num; i++.) at { - intv=G[u][i]; - if(V! = father[0][u]) DFS (v,u,depth+1); - } - } - in voidInit () - { toDFS (root,-1,0); + for(intk=0; k +1<MAX_LOG_MAXN; k++) - { the for(intI=1; i<=n; i++.) * { $ if(father[k][i]<0) father[k+1][i]=-1;Panax Notoginseng Elsefather[k+1][i]=Father[k][father[k][i]]; - } the } + } A the intLCA () + { - if(d[a]<d[b]) swap (A, b); $ for(intk=0; k<max_log_maxn; k++.) $ { - if((D[a]-d[b]) >>k &1) - { theA=Father[k][a]; - }Wuyi } the if(a==b)returnA; - for(intk=max_log_maxn-1; k>=0; k--) Wu { - if(Father[k][a]! =Father[k][b]) About { $A=Father[k][a]; -b=Father[k][b]; - } - } A returnfather[0][a]; + } the - intMain () $ { the intT; thescanf"%d",&t); the while(t--) the { -scanf"%d",&n); in for(intI=0; i<=n; i++.) g[i].clear (); the for(intI=0; i<max_log_maxn; i++.) the { About for(intj=0; j<maxn; j + +) thefather[i][j]=-1; the } the intb; + intVIS[MAXN]; -memset (Vis,0,sizeof(Vis)); the for(intI=0; i<n-1; i++)Bayi { thescanf"%d%d",&a,&b); the G[a].push_back (b); -vis[b]=1; - } thescanf"%d%d",&a,&B); the for(intI=1; i<=n; i++)if(!vis[i]) {root=i; Break; } the init (); theprintf"%d\n", LCA ()); - } the return 0; the}
POJ 1330 Nearest Common Ancestors LCA