[Recent public ancestors] POJ 1330 Nearest Common Ancestors

Source: Internet
Author: User

Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27316 Accepted: 14052

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

Source

Taejon 2002 Main idea: seek the nearest public ancestor. Problem-solving ideas: Multiply or tarjian+ and check the offline request. The multiplication algorithm is given below to calculate the LCA.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int num,frist[20010],  Node[20010],deep[20010],ancestor[20010][17],n;struct MP {int to,next;   }map[20010];void init () {num=0;   memset (ancestor,0,sizeof (ancestor));   Memset (Frist,0,sizeof (frist));   memset (deep,0,sizeof (deep));    memset (node,0,sizeof (node));  memset (map,0,sizeof (map));   }void Add (int x,int y) {++num;   Map[num].to=y;  Map[num].next=frist[x];frist[x]=num;   }void build (int v) {int i;            for (I=frist[v];i;i=map[i].next) {if (!deep[map[i].to]) {ancestor[map[i].to][0]=v;           deep[map[i].to]=deep[v]+1;         Build (map[i].to);   }}}void Init_ancestor () {int i,j; for (J=1;J&LT;17;++J) for (i=1;i<=n;++i) if (ancestor[i][j-1]) ancestor[i][j]=ancestor[ancestor[i][j-1]]   [J-1];  Return  }int LCA (int a,int b) {int i,dep;  if (Deep[a]<deep[b]) swap (A, b);  DEP=DEEP[A]-DEEP[B]; for (I=0;i<17;++i) if ((1<<i) &AMP;DEP) a=ancestor[a][i];  if (a==b) return A;         for (i=16;i>=0;--i) {if (Ancestor[a][i]!=ancestor[b][i]) {a=ancestor[a][i];   B=ancestor[b][i];  }}return ancestor[a][0];   }int Main () {int t,i,v,w,roof,qv,qw;   scanf ("%d", &t);       while (t--) {init ();       scanf ("%d", &n);           for (i=0;i<n-1;++i) {scanf ("%d%d", &v,&w);           Add (v,w); add (w,v);           Ancestor[w][0]=v; if (!ancestor[v][0]) roof=v;//find root, the root of the different answer is different.  } deep[roof]=1;  Build (roof);  Init_ancestor ();  scanf ("%d%d", &AMP;QV,&AMP;QW);   printf ("%d\n", LCA (QV,QW));  } return 0; }

  

[Recent public ancestors] POJ 1330 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.