Description:
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:
4
3
Pure template problem, test template, stay with.
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include < Math.h> #include <vector>using namespace Std;const int maxn=10010;//The number of nodes in the tree int f[maxn];//and check set int r[maxn];// And check the number of sets of the set bool vis[maxn];//access tag int ancestor[maxn];//ancestor struct node{int to, next;} Edge[maxn*2];int head[maxn];int tol;void addedge (int a,int b) {edge[tol].to=b; Edge[tol].next=head[a]; head[a]=tol++; Edge[tol].to=a; EDGE[TOL].NEXT=HEAD[B]; head[b]=tol++;} struct query{int q,next; int index;//query number}query[maxn*2];//query int ans[maxn*2];//query result int cnt;int h[maxn];int tt;int q;//query number void add_query (int a, int b,int i)//edge A to B, i-th query {query[tt].q=b; Query[tt].next=h[a]; Query[tt].index=i; h[a]=tt++; Query[tt].q=a; QUERY[TT].NEXT=H[B]; Query[tt].index=i; h[b]=tt++;} void init (int n)//incoming n is the total number of nodes {for (int i=1;i<=n;i++) {f[i]=-1; R[i]=1; Vis[i]=false; ancestor[i]=0; tol=0; tt=0;The number of cnt=0;//already queried} memset (Head,-1,sizeof (head)); memset (h,-1,sizeof (h)); int find (int x) {if (f[x]==-1) return x; Return F[x]=find (F[x]);} void Union (int x,int y)//merge {int t1=find (x); int T2=find (y); if (T1!=T2) {if (R[t1]<=r[t2]) {f[t1]=t2; R[T2]+=R[T1]; } else {f[t2]=t1; R[T1]+=R[T2]; }}}void LCA (int u) {//if (cnt>=q) return;//do not add this ancestor[u]=u; vis[u]=true;//this must be put in front for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].to; if (Vis[v]) continue; LCA (v); Union (U,V); Ancestor[find (u)]=u; } for (int i=h[u];i!=-1;i=query[i].next) {int v=query[i].q; if (Vis[v]) {Ans[query[i].index]=ancestor[find (v)]; cnt++;//the number of answers that have been found}}}bool flag[maxn];int main () {int t;scanf ("%d", &t), while (t--) {int N;memset (flag, True, si ZEOF (flag)); scanf ("%d", &n); init (N); int U, v; for (int i=1;i<n;i++) {scanf ("%d%d", &u, &v); Flag[v] = False;addedge (U, v);} Q = 1;scanf ("%d%d", &u, &v); Add_query (U, V, 1); int root;for (int i=1;i<=n;i++) if (flag[i]) root = i; LCA (Root), for (int i=1;i<=q;i++) printf ("%d\n", Ans[i]);} return 0;}
POJ 1330 Nearest Common ancesters (Lca,tarjan offline algorithm)