A - Nearest Common Ancestors

A - Nearest Common Ancestors

  A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:

A - Nearest Common Ancestors
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的题我都是用倍增法来做的,但是在网上查题的时候就发现有很多大佬都是用Tarjan算法来做的,

而且Tarjan算法的复杂度同时也更加的低,效率更加的优秀,之后有时间的话一定要把这些题目再用Tarjan算法的方法来再做一遍

 

代码如下:

ps:这道题是POJ上面的题,所以头文件不能用万能头。。。。

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <stack>
  5 #include <queue>
  6 #include <map>
  7 #include <set>
  8 #include <vector>
  9 #include <math.h>
 10 #include <bitset>
 11 #include <algorithm>
 12 #include <climits>
 13 #define res register int
 14 #define ll long long
 15 #define maxn 10050
 16 using namespace std;
 17 struct Node
 18 {
 19     int to,next;
 20 };
 21 Node e[maxn<<1];
 22 int father[maxn][30],head[maxn],lg[maxn],depth[maxn],n;
 23 bool vis[maxn];
 24 int tot;
 25 
 26 inline void add(int x,int y)
 27 {
 28     e[tot].to=y;
 29     e[tot].next=head[x];
 30     head[x]=tot++;
 31 }
 32 
 33 inline void dfs(int now,int fa)
 34 {
 35     depth[now]=depth[fa]+1;
 36     father[now][0]=fa;
 37     for(int i=1; (1<<i)<=depth[now]; ++i)
 38     {
 39         father[now][i]=father[father[now][i-1]][i-1];
 40     }
 41 
 42     for(int i=head[now]; i; i=e[i].next)
 43     {
 44         if(e[i].to!=fa)
 45             dfs(e[i].to,now);
 46     }
 47 }
 48 
 49 int lca(int a,int b)
 50 {
 51     if(depth[a]<depth[b]) swap(a,b);
 52     int t=depth[a]-depth[b];
 53     for(int i=0;i<=20;i++)
 54     {
 55         if((1<<i)&t)
 56             a=father[a][i];
 57     }
 58     if(a==b) return a;
 59     for(int i=20;i>=0;i--)
 60     {
 61         if(father[a][i]!=father[b][i])
 62         {
 63             a=father[a][i];
 64             b=father[b][i];
 65         }
 66     }
 67     return father[a][0];
 68 }
 69 
 70 
 71 int main()
 72 {
 73     int T;
 74     cin>>T;
 75     while(T--)
 76     {
 77         tot=1;
 78         cin>>n;
 79         memset(depth,0,sizeof(depth));
 80         memset(father,0,sizeof(father));
 81         memset(head,-1,sizeof(head));
 82         memset(vis,true,sizeof(vis));
 83         for(int i=1; i<n; ++i)
 84         {
 85             int x,y;
 86             cin>>x>>y;
 87             add(x,y);
 88             add(y,x);
 89             vis[y]=false;
 90         }
 91         for(int i=1; i<=n; ++i)
 92         {
 93             if(vis[i])
 94             {
 95                 dfs(i,0);
 96             }
 97 
 98         }
 99         for(int i=1;i<=n;++i)
100             lg[i]=lg[i-1]+(1<<lg[i-1]==i);
101         int sa,sb;
102         cin>>sa>>sb;
103         cout<<lca(sa,sb)<<endl;
104     }
105     return 0;
106 }
上一篇:十大常用算法


下一篇:cs231n__2. K-nearest Neighbors