Poj 1330 Nearest Common Ancestors (multiplication method)

Source: Internet
Author: User


Nearest Common Ancestors
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 14078 Accepted: 7510

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

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

Source

Taejon 2002

Online algorithm Multiplication

O (logN)

D [I] indicates the depth of the I node. p [I, j] indicates the 2 ^ j times ancestor of I.

Then there is a recursive formula p [I, j] = p [p [I, J-1], J-1]

In this way, an O (NlogN) preprocessing is used to obtain the 2 ^ k ancestor of each node.

Then, for each query point, the nearest common ancestor of B is:

First, determine whether d [a]> d [B]. If yes, exchange it (ensure that the depth of a is smaller than B to facilitate the following operations) then adjust B to the same depth as a, and then adjust a and B to the same depth (dec (j) at the same time to a minimum j to meet the requirements of p [a, j]. = P [B, j] (a B is constantly updated), and finally the and B are upgraded (a = p [a, 0], B = p [B, 0]) One by one up to a = B, then a or B is their recent public ancestor

Http://poj.org/problem? Id = 1330


[Analysis]


The general idea of doubling the method to evaluate the LCA is as follows:


First, determine the depth d [I], indicating the distance from I to the root node or the depth.


P [I, j] indicates the ascending 2nd ^ j ancestor of I, then p [I, j + 1]: = p [p [I, j], j];


Dfs calculates d [I] and p [I, j].


If the result is the lca of x and y. If x = y, lca = x or y;


If the depth of x is different from that of y, it is adjusted to the same depth to evaluate the lca.


If the depth of x and y is the same, we start to go up. If the two points go through the same number of steps at the same time and reach a point, then this point is the lca. Just imagine, this is obvious. What is the first similarity between the two points?


End.

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <vector>
Using namespace std;
# Define N 10010
# Define M 20
Int d [N], f [N] [M];
Vector <int> ch [N];
Void dfs (int x) {// obtain the depth of all nodes
D [x] = d [f [x] [0] + 1;
For (int I = 1; I <M; I ++) f [x] [I] = f [f [x] [I-1] [I-1]; // multiply the ancestor
For (int I = ch [x]. size ()-1; I> = 0; I --)
Dfs (ch [x] [I]); // traverse the Son
}
Int lca (int x, int y) {// returns the minimum common ancestor of x and y.
If (d [x] <d [y]) swap (x, y); // ensure that the depth of x is not less than y
Int k = d [x]-d [y];
For (int I = 0; I <M; I ++)
If (1 <I) & k)
X = f [x] [I];
If (x = y) return x; // x = y is the minimum common ancestor.
For (int I = M-1; I> = 0; I --)
If (f [x] [I]! = F [y] [I]) {
X = f [x] [I]; y = f [y] [I];
}
Return f [x] [0];
}
Int main (){
Int T, n, I, x, y;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
Memset (d, 0, sizeof (d ));
Memset (f, 0, sizeof (f ));
Memset (ch, 0, sizeof (ch ));
For (I = 1; I <n; I ++ ){
Scanf ("% d", & x, & y );
Ch [x]. push_back (y );
F [y] [0] = x;
}
For (I = 1; I <= n; I ++)
If (f [I] [0] = 0) {// find the root Traversal
Dfs (I); break;
}
Scanf ("% d", & x, & y );
Printf ("% d \ n", lca (x, y ));
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <vector>
Using namespace std;
# Define N 10010
# Define M 20
Int d [N], f [N] [M];
Vector <int> ch [N];
Void dfs (int x) {// obtain the depth of all nodes
D [x] = d [f [x] [0] + 1;
For (int I = 1; I <M; I ++) f [x] [I] = f [f [x] [I-1] [I-1]; // multiply the ancestor
For (int I = ch [x]. size ()-1; I> = 0; I --)
Dfs (ch [x] [I]); // traverse the Son
}
Int lca (int x, int y) {// returns the minimum common ancestor of x and y.
If (d [x] <d [y]) swap (x, y); // ensure that the depth of x is not less than y
Int k = d [x]-d [y];
For (int I = 0; I <M; I ++)
If (1 <I) & k)
X = f [x] [I];
If (x = y) return x; // x = y is the minimum common ancestor.
For (int I = M-1; I> = 0; I --)
If (f [x] [I]! = F [y] [I]) {
X = f [x] [I]; y = f [y] [I];
}
Return f [x] [0];
}
Int main (){
Int T, n, I, x, y;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
Memset (d, 0, sizeof (d ));
Memset (f, 0, sizeof (f ));
Memset (ch, 0, sizeof (ch ));
For (I = 1; I <n; I ++ ){
Scanf ("% d", & x, & y );
Ch [x]. push_back (y );
F [y] [0] = x;
}
For (I = 1; I <= n; I ++)
If (f [I] [0] = 0) {// find the root Traversal
Dfs (I); break;
}
Scanf ("% d", & x, & y );
Printf ("% d \ n", lca (x, y ));
}
Return 0;
}
 

 

 

Related Article

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.