Nearest Common Ancestors

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

模板题啦,熟悉一下LCA的写法。

 1 #pragma warning(disable:4996)
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 1e4 + 4;
 9 
10 int T, n, tot, start;
11 int head[maxn], root[maxn], pa[16][maxn], dp[maxn];
12 
13 struct node {
14     int to, next;
15 }e[2*maxn];
16 
17 void Inite() {
18     tot = 0;
19     memset(root, -1, sizeof(root));
20     memset(head, -1, sizeof(head));
21 }
22 
23 void addedge(int u, int v) {
24     e[tot].to = v;
25     e[tot].next = head[u];
26     head[u] = tot++;
27 }
28 
29 void DFS(int u, int p, int deep) {
30     dp[u] = deep;
31     pa[0][u] = p;
32     for (int i = head[u]; i != -1; i = e[i].next) {
33         int v = e[i].to;
34         if (v == p) continue;
35         DFS(v, u, deep + 1);
36     }
37 }
38 
39 void InitP() {
40     DFS(start, -1, 0);
41     //for (int i = 1; i <= n; i++) cout << dp[i] << " ";
42     //cout << endl;
43     for (int k = 0; k + 1 < 16; k++) {
44         for (int v = 1; v <= n; v++) {
45             if (pa[k][v] < 0) pa[k + 1][v] = -1;
46             else pa[k + 1][v] = pa[k][pa[k][v]];
47         }
48     }
49 }
50 
51 int LCA(int u, int v) {
52     if (dp[u] > dp[v]) swap(u, v);
53     for (int k = 0; k < 16; k++) if ((dp[v] - dp[u]) >> k & 1) v = pa[k][v];
54     if (v == u) return v;
55     for (int k = 15; k >= 0; k--) {
56         if (pa[k][v] != pa[k][u]) {
57             v = pa[k][v];
58             u = pa[k][u];
59         }
60     }
61     return pa[0][v];
62 }
63 
64 int main()
65 {
66     cin >> T;
67     while (T--) {
68         Inite();
69 
70         cin >> n;
71         for (int i = 2; i <= n; i++) {
72             int u, v;
73             cin >> u >> v;
74             addedge(u, v);
75             addedge(v, u);
76             root[v] = u;
77         }
78 
79         for (int i = 1; i <= n; i++) if (root[i] == -1) { start = i; break; }
80         InitP();
81 
82         int u, v;
83         cin >> u >> v;
84         printf("%d\n", LCA(u, v));
85     }
86     return 0;
87 }

 

posted @ 2018-03-27 23:33  天之道,利而不害  阅读(184)  评论(0编辑  收藏  举报