Nearest common ancestors · poj1330

Source: Internet
Author: User

Door: http://poj.org/problem? Id = 1330

Nearest common ancestors
Time limit:1000 ms   Memory limit:10000 K
     

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 labeled with an integer from {1, 2 ,..., 16 }. node 8 is the root of the tree. node X is an ancestor of node y If node X is in the path between the root and node y. for example, node 4 is an ancestor of node 16. node 10 is also an ancestor of node 16. as a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. remember that a node is 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 two 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 16 and 7. A node X is called the nearest common ancestor of nodes Y and Z if X is a common ancestor of Y and Z and nearest to Y and Z among their common ancestors. hence, the nearest common ancestor of nodes 16 and 7 is node 4. node 4 is nearer to nodes 16 and 7 than node 8 is.

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. in the last example, if y is an ancestor of Z, then the nearest common ancestor of Y and Z is Y.

Write a program that finds the nearest common ancestor of two distinct nodes in a tree.

Input

The input consists of T test cases. the number of test cases (t) is given in the first line of the input file. each test case starts with a line containing an integer N, the number of nodes in a tree, 2 <=n <= 10,000. the nodes are labeled with integers 1, 2 ,..., n. each of the next n-1 lines contains a pair of integers that represent an edge -- the first integer is the parent node of the second integer. note that a tree with N nodes has exactly n-1 edges. the last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line shoshould contain the integer that 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 has nothing to say .. Pay attention to multiple sets of data codes:
 1 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 10010;10 #define For(i,n) for(int i=1;i<=n;i++)11 #define Rep(i,l,r) for(int i=l;i<=r;i++)12 13 struct Edge{14     int t,next;15 }E[N];16 17 int in[N],head[N],Es;18 int n,T,x,y,ans,qx,qy,root,anc[N],fa[N];19 bool vis[N];20 21 void makelist(int s,int t){22     E[Es].t = t;E[Es].next = head[s];23     head[s] = Es++;24 }25 26 int find(int i){27     return (fa[i]==i)?(i):(find(fa[i]));28 }29 30 void LCA(int i){31     if(ans!=0) return;32     anc[i] = i;33     for(int p = head[i];p!=-1;p=E[p].next){34         LCA(E[p].t);35         fa[find(E[p].t)] = find(i);36         anc[find(i)] = i;37     }38     vis[i] = true;39     if(i==qx)40         if(vis[qy]) ans = anc[find(qy)];41     if(i==qy)42         if(vis[qx]) ans = anc[find(qx)];43 }44 45 int main(){46     scanf("%d",&T);47     For(i,T){48         memset(head,-1,sizeof(head));Es = 0;ans = 0;49         memset(in,0,sizeof(in));memset(anc,0,sizeof(anc));50         memset(vis,false,sizeof(vis));51         scanf("%d",&n);52         For(i,n-1){53             fa[i] = i;54             scanf("%d %d",&x,&y);55             makelist(x,y);56             in[y]++;57         }58         fa[n] = n;59         For(i,n)60             if(!in[i]) {root = i;break;}61         scanf("%d %d",&qx,&qy);62         LCA(root);63         printf("%d\n",ans);64     }65     return 0;66 }

 

 

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.