Topic Connection
http://poj.org/problem?id=1330
Nearest Common ancestorsdescription
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 is-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
2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
00a2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5
Sample Output
4
3
LCA naked question, test template:
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < Cstdio> #include <vector>using namespace std;const int N = 10010;struct Tarjan_lac {bool vis[n];struct edge {int To, next; }g[n << 1];int A, B, tot, ans, root, par[n], head[n];inline void init (int N) {ans = tot = 0;for (int i = 0; i < N + 2; i++) {Par[i] = i;vis[i] = false;head[i] =-1;}} inline void Add_edge (int u, int v) {g[tot].to = V, g[tot].next = Head[u], head[u] = tot++; g[tot].to = u, G[tot].next = Head[v], head[v] = tot++;} inline void built (int n) {int u, v;for (int i = 0; i < n-1; i++) {scanf ("%d%d", &u, &v); Add_edge (U, v); vis[v ] = true;} for (int i = 1; I <= n; i++) {if (!vis[i]) root = i;} Memset (Vis, false, sizeof (VIS)), scanf ("%d%d", &a, &b); inline int find (int x) {while (x = Par[x]) {x = par[x] = par[par[x]];} return x;} inline void Tarjan (int u, int fa) {for (int i = head[u]; ~i; i = g[i].next) {int &v = G[i].to;if(v = = FA) Continue;tarjan (V, u); vis[v] = true;par[v] = u;} if (U = = A && vis[b]) {ans = find (B);} if (U = = B && Vis[a]) {ans = find (A);}} inline void Solve (int n) {init (n); built (n); Tarjan (root, root);p rintf ("%d\n", ans);} Go;int Main () {#ifdef localfreopen ("In.txt", "R", stdin), Freopen ("OUT.txt", "w+", stdout), #endifint T, n;scanf ("%d", & AMP;T); while (t--) {scanf ("%d", &n); Go.solve (n);} return 0;}
POJ 1330 Nearest Common Ancestors