POJ1330 Nearest Common Ancestors

Source: Internet
Author: User

Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 24587 Accepted: 12779

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 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.

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

Taejon 2002

LCA (recent public ancestor)

Find the root of the tree first, then multiply the ancestor nodes of each point, then back up.

1 //LCA2#include <algorithm>3#include <iostream>4#include <cstdio>5#include <cstring>6#include <cmath>7#include <vector>8 using namespacestd;9 Const intmxn=10010;Ten intN; Onevector<int>E[MXN]; A intfa[mxn][ -]; - intDEP[MXN]; - int inch[MXN]; the voidAdd_edge (intUintv) { - E[u].push_back (v); -     inch[v]++; - } + voidDfsintu) {//to find the depth of a point, multiply the ancestor node FA . -      for(intI=0; I<e[u].size (); i++){ +         intv=E[u][i]; Afa[v][0]=u; atdep[v]=dep[u]+1; -          for(intj=1;(1&LT;&LT;J) <=dep[v];j++){ -fa[v][j]=fa[fa[v][j-1]][j-1]; -         } - Dfs (v); -     } in     return; - } to intLcaintAintb) { +     if(Dep[a]<dep[b]) swap (A, b);//(the one with the greater depth) -      for(intI= -; i!=-1; i--)//try from the high. (In fact, from the low start is also the same?) ) the         if(Dep[a]>=dep[b]+ (1<<i)) A=fa[a][i];//at the end of this step, A and B find the same depth *     if(a==b)returnA; $      for(intI= -; i!=-1; i--)//co-upstreamPanax Notoginseng         if(Fa[a][i]!=fa[b][i]) a=fa[a][i],b=Fa[b][i]; -     returnfa[a][0]; the } + intMain () { A     intT; thescanf"%d",&T); +     inti,j; -      while(t--){ $memset (DEP,0,sizeof(DEP)); $memset (FA,0,sizeof(FA)); -Memsetinch,0,sizeof(inch)); -scanf"%d",&n); the          for(i=1; i<=n;i++) e[i].clear (); -         intu,v;Wuyi          for(i=1; i<n;i++){ thescanf"%d%d",&u,&v); - Add_edge (u,v); Wu         } -         introot=0; About          for(i=1; i<=n;i++)if(inch[i]==0) {root=i; Break;}//The point at which the entry is 0 is the root $dep[root]=1; -fa[root][0]=-1; - Dfs (root); -         intb; Ascanf"%d%d",&a,&b); +printf"%d\n", LCA (A, b)); the     } -     return 0; $}

POJ1330 Nearest Common Ancestors

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.