Poj1330nearest Common ancestors and explaining the multiplication method to seek LCA

Source: Internet
Author: User

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20487 Accepted: 10784

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

2161 148 510 165 94 68 44 101 136 1510 116 710 216 38 116 1216 752 33 43 11 53 5

Sample Output

43

Source

Taejon 2002

The multiplication method is probably about converting an O (n) problem to log (n).
LCA is the nearest public ancestor to two nodes in a tree.
The multiplication method for LCA is done in this way:
Two arrays are preprocessed first
PR[I][J]: 2^j ancestor node of node I
Dth[i]: Node I depth in the tree
This can be recursive with the search, recursion is such a pr[i][j]=pr[pr[i][j-1]][j-1],
How did it come about? The first 2^j ancestor of I is the first 2^ (j-1) ancestor of the 2^ (j-1) Ancestors of I,
Because 2^ (j-1) +2^ (j-1) =2*2^ (j-1) =2^j

When one,two the LCA of these two nodes,
How do you increase the multiplication to the same depth first?
For convenience of description, guarantee Dth[one]>dth[two],
Figure out the depth difference between them re=dth[one]-dth[two],
Starting from the lowest bit of the binary of the RE, go to the highest level,
If you encounter 1, assuming that this is the first bit, then One-2^i,
At the end, one is at the same depth as two, and here is a clever use of the binary addition and subtraction operation

We can also increase the depth of the one,two, if there is a total of n nodes,
We enumerate their first 2^i ancestors, starting with the log (N) enumeration to 0.
When Pr[one][i]!=pr[two][i], then one=pre[one][i],two=pre[two][i],
So finally, Pre[one][0] is their LCA.
Why is it possible to do so? Obviously if Pr[one][i]==pr[two][i]
Then, Pr[one][i] is the public ancestor of One,two, but not necessarily the nearest, so do not tube,
If pr[one][i]! =pr[two][i] then pr[one][i] and pr[two][i] must still be in the sub-tree of LCA,
Continue to run up on the line, and each run, obviously closer to the LCA, I must be smaller than before,
So the direct enumeration to 0 on the line, to the last one and the other will certainly run to the son of LCA, so
At this point, one and two of the first 2^0 ancestors, namely their fathers, are LCA.



The problem is to find a tree with a junction of 0, a pair of nodes of the LCA
#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int head[10010],tail;struct edge{int to,next;} edge[20010];void Add (int from,int to) {edge[tail].to=to;edge[tail].next=head[from];head[from]=tail++;} int pr[10010][20],dth[10010];void BFS (int st) {Queue<int>qq;qq.push (ST); memset (pr,0,sizeof (pr));d th[st]=1; while (Qq.size ()) {int From=qq.front (), Qq.pop (), for (int i=head[from];i!=-1;i=edge[i].next) {int to=edge[i].to;if (to! =pr[from][0]) {dth[to]=dth[from]+1;pr[to][0]=from;for (int i=1; (1<<i) <=dth[from];i++) Pr[to][i]=pr[pr[to] [I-1]] [I-1];qq.push (To);}}}} int LCA (int one,int) {if (Dth[one]<dth[two]) swap (one,two); int re=dth[one]-dth[two];for (int i=0; (re>>i)! = 0;i++) if ((re>>i) &1) one=pr[one][i];if (one!=two) {for (int i=15;i>-1;i--) if (Pr[one][i]!=pr[two][i]) {one=pr[ One][i];two=pr[two][i];} ONE=PR[ONE][0];} return one;} BOOL Flag[10010];int Main () {int T;cin>>t;while (t--) {int N;cin>>n;tail=0;memset (head,-1,sizeof (head)); memset (flag,0,sizeof (flag)), for (int i=1;i<n;i++) {int from,to;cin>>from>>to;add (from,to); from); flag[to]=1;} for (int i=1;i<=n;i++) if (!flag[i]) {BFS (i); int One,two;cin>>one>>two;cout<<lca (one,two) <<endl;}}


Poj1330nearest Common ancestors and explaining the multiplication method to seek LCA

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.