【lca】lca转rmq poj1330

来源:互联网 发布:中国程序员人才需求 编辑:程序博客网 时间:2024/04/30 00:02

lca一般求法都是tarjan 或者 倍增,其实还可以转化为rmq来求解。具体如下

一:

1.对有根树T进行DFS,将遍历到的结点按照顺序记下,我们将得到一个长度为2N – 1的序列,称之为T的欧拉序列F
2.每个结点都在欧拉序列中出现,我们记录结点u在欧拉序列中第一次出现的位置为pos(u),我们也用一个depth[]来记录对应的欧拉序列的元素的深度。
例如:


根据DFS的性质,对于两结点u、v,从pos(u)遍历到pos(v)的过程中经过LCA(u, v)有且仅有一次,且深度是深度序列B[pos(u)…pos(v)]中最小的,即LCA(T, u, v) = RMQ(B, pos(u), pos(v)),并且问题规模仍然是O(N)的,这就证明了LCA问题是可以转化为RMQ问题的。具体的大家可以参看2007年国家集训队论文。
还是以一道题作为模板,poj1330
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11136
Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u

Submit Status

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

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


【代码】:
#include<cstdio>#include<iostream>#include<vector>#include<cstring>#include<cmath>#define mem(a,x) memset(a, x, sizeof(a))using namespace std;vector<int >e[10000 + 5];int pos[10000 + 5];//第一个出现的位置 int depth[10005 << 1];//出现的深度 //要开成两倍int dpmin[10000 + 5][30];//表示以i开始,持续2^j个最小值的下标 int r[10005 << 1];//出现的顺序 //要开成两倍int ind[10000 + 5];//入度判断 int root;int T;int n, m;void init()//初始化{m = 0;for(int i = 1; i <= n; i++){ind[i] = 0;e[i].clear();}mem(pos, 0);mem(depth,0);mem(r, 0);}void dfs(int u, int deep){r[++m] = u;//访问到时进行记录 depth[m] = deep;if(!pos[u]) pos[u] = m;for(int i = 0; i < e[u].size(); i++){dfs(e[u][i],deep + 1);r[++m] = u;//回溯时也记录 depth[m] = deep;}}void RMQ()//基本的rmq的算法{for(int i = 1; i <= m; i++)dpmin[i][0] = i;for(int j = 1; (1 << j) <= m; j++)for(int i = 1; i <= m; i++)if(i + (1 << j) - 1 <= m){if(depth[dpmin[i][j - 1]] < depth[dpmin[i + (1 << (j - 1))][j - 1]])dpmin[i][j] = dpmin[i][j - 1];//注意,保存的是深度最小值的下标else dpmin[i][j] = dpmin[i + (1 << (j - 1))][j - 1];}}int work(int a, int b)//基本的rmq的算法{int l = pos[a], r1 = pos[b];if(l > r1)swap(l, r1);int k = (int)(log((double)(r1 - l + 1)) / log(2.0));if(depth[dpmin[l][k]] < depth[dpmin[r1 - (1 << k) + 1][k]]) return r[dpmin[l][k]];else return r[dpmin[r1 - (1 << k) + 1][k]];}int main(){scanf("%d", &T);while(T--){init();scanf("%d", &n);for(int i = 1; i < n; i++){int u, v;scanf("%d%d", &u, &v);e[u].push_back(v);//存入边ind[v]++;}for(int i = 1; i <= n; i++)if(!ind[i]){root = i;break;//入度为0就是根}dfs(root,0);//dfs一边RMQ();int a, b;scanf("%d%d", &a, &b);printf("%d\n", work(a,b));}return 0;}



0 0