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 should contain the integer that is the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5

Sample Output

4
3

题解:LCA最近公共祖先裸题,直接套模板,可以用vector模拟邻接表或者链式前向星

 vector模拟邻接表代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <stdlib.h>
  4 #include <cmath>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <string.h>
  8 #include <vector>
  9 #include <queue>
 10 #include <stack>
 11 #include <set>
 12 #include <map>
 13 #include <ctime>
 14 #define maxn 32007
 15 #define N 200005
 16 #define INF 0x3f3f3f3f
 17 #define PI acos(-1)
 18 #define lowbit(x) (x&(-x))
 19 #define eps 0.000000001
 20 #define read(x) scanf("%d",&x)
 21 #define put(x) printf("%d\n",x)
 22 #define memset(x,y) memset(x,y,sizeof(x))
 23 #define Debug(x) cout<<x<<" "<<endl
 24 #define lson i << 1,l,m
 25 #define rson i << 1 | 1,m + 1,r
 26 using namespace std;
 27 typedef long long ll;
 28 
 29 const int MAXL(1e5);
 30 
 31 int father[MAXL+50];
 32 bool is_root[MAXL+50];
 33 bool vis[MAXL+50];
 34 vector<int>v[MAXL+50];
 35 int root;
 36 int cx,cy;
 37 int ans;
 38 int find(int x)
 39 {
 40     if(x!=father[x])
 41         father[x]=find(father[x]);
 42     return father[x];
 43 }
 44 
 45 void Join(int x,int y)
 46 {
 47     int fx=find(x),fy=find(y);
 48     if(fx!=fy)
 49         father[fy]=fx;
 50 }
 51 
 52 void LCA(int u)
 53 {
 54     for(int i=0; i<v[u].size(); i++)
 55     {
 56         int child=v[u][i];
 57         if(!vis[child])
 58         {
 59             LCA(child);
 60             Join(u,child);
 61             vis[child]=true;
 62         }
 63     }
 64     if(u==cx&&vis[cy]==true)
 65         ans=find(cy);
 66     if(u==cy&&vis[cx]==true)
 67         ans=find(cx);
 68 
 69 }
 70 
 71 void init()
 72 {
 73     memset(is_root,true);
 74     memset(vis,false);
 75     int n;
 76     scanf("%d",&n);
 77     for(int i=0; i<=n; i++)
 78         v[i].clear();
 79     for(int i=1; i<=n; i++)
 80         father[i]=i;
 81     for(int i=1; i<n; i++)
 82     {
 83         int x,y;
 84         scanf("%d%d",&x,&y);
 85         v[x].push_back(y);
 86         is_root[y]=false;
 87     }
 88     scanf("%d%d",&cx,&cy);
 89     for(int i=1; i<=n; i++)
 90     {
 91         if(is_root[i]==true)
 92         {
 93             root=i;
 94             break;
 95         }
 96     }
 97 
 98 }
 99 int main()
100 {
101     int t;
102     scanf("%d",&t);
103     while(t--)
104     {
105         init();
106         LCA(root);
107         cout<<ans<<endl;
108     }
109 }
View Code

链式前向星代码如下:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<vector>
  6 #include<queue>
  7 #define eps 1e-8
  8 #define memset(a,v) memset(a,v,sizeof(a))
  9 using namespace std;
 10 typedef long long int LL;
 11 const int MAXL(1e6);
 12 const int INF(0x7f7f7f7f);
 13 const int mod(1e9+7);
 14 int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
 15 struct node
 16 {
 17     int to;
 18     int next;
 19 }edge[MAXL+50];
 20 int head[MAXL+50];
 21 int father[MAXL+50];
 22 bool vis[MAXL+50];
 23 bool is_root[MAXL+50];
 24 int n;
 25 int cnt;
 26 int cx,cy;
 27 int ans;
 28 int root;
 29  
 30  
 31 int Find(int x)
 32 {
 33     if(x!=father[x])
 34         father[x]=Find(father[x]);
 35     return father[x];
 36 }
 37  
 38 void Join(int x,int y)
 39 {
 40     int fx=Find(x),fy=Find(y);
 41     if(fx!=fy)
 42         father[fy]=fx;
 43 }
 44  
 45 void add_edge(int x,int y)
 46 {
 47     edge[cnt].to=y;
 48     edge[cnt].next=head[x];
 49     head[x]=cnt++;
 50 }
 51  
 52 void init()
 53 {
 54     cnt=0;
 55     memset(head,-1);
 56     memset(vis,false);
 57     memset(is_root,true);
 58     scanf("%d",&n);
 59     for(int i=0;i<=n;i++)
 60         father[i]=i;
 61     for(int i=1;i<n;i++)
 62     {
 63         int x,y;
 64         scanf("%d%d",&x,&y);
 65         add_edge(x,y);
 66         is_root[y]=false;
 67     }
 68     for(int i=1;i<=n;i++)
 69         if(is_root[i]==true)
 70             root=i;
 71 }
 72  
 73 void LCA(int u)
 74 {
 75     for(int i=head[u];~i;i=edge[i].next)
 76     {
 77         int v=edge[i].to;
 78         LCA(v);
 79         Join(u,v);
 80         vis[v]=true;
 81  
 82     }
 83     if(cx==u&&vis[cy]==true)
 84         ans=Find(cy);
 85     if(cy==u&&vis[cx]==true)
 86         ans=Find(cx);
 87 }
 88 void solve()
 89 {
 90     scanf("%d%d",&cx,&cy);
 91     LCA(root);
 92 }
 93 int main()
 94 {
 95     int T;
 96     scanf("%d",&T);
 97     while(T--)
 98     {
 99         init();
100         solve();
101         cout<<ans<<endl;
102     }
103 }
104  
View Code

 

posted on 2018-08-03 16:34  Baiyi_destroyer  阅读(113)  评论(0编辑  收藏  举报