【lca】lca转rmq写法 poj1330

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
                                                                                        Nearest Common Ancestors
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u

 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

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

 代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<cstring>
 5 #include<cmath>
 6 #define mem(a,x) memset(a, x, sizeof(a))
 7 using namespace std;
 8 
 9 vector<int >e[10000 + 5];
10 int pos[10000 + 5];//第一个出现的位置 
11 int depth[10005 << 1];//出现的深度 //要开成两倍
12 int dpmin[10000 + 5][30];//表示以i开始,持续2^j个最小值的下标 
13 int r[10005 << 1];//出现的顺序 //要开成两倍
14 int ind[10000 + 5];//入度判断 
15 int root;
16 int T;
17 int n, m;
18 
19 void init()//初始化
20 {
21     m = 0;
22     for(int i = 1; i <= n; i++)
23     {
24         ind[i] = 0;
25         e[i].clear();
26     }
27     mem(pos, 0);
28     mem(depth,0);
29     mem(r, 0);
30 }
31 void dfs(int u, int deep)
32 {
33     r[++m] = u;//访问到时进行记录 
34     depth[m] = deep;
35     if(!pos[u]) pos[u] = m;
36     for(int i = 0; i < e[u].size(); i++)
37     {
38         dfs(e[u][i],deep + 1);
39         r[++m] = u;//回溯时也记录 
40         depth[m] = deep;
41     }
42 }
43 void RMQ()//基本的rmq的算法
44 {
45     for(int i = 1; i <= m; i++)
46     dpmin[i][0] = i;
47     for(int j = 1; (1 << j) <= m; j++)
48     for(int i = 1; i <= m; i++)
49         if(i + (1 << j) - 1 <= m)
50         {
51             if(depth[dpmin[i][j - 1]] < depth[dpmin[i + (1 << (j - 1))][j - 1]])
52             dpmin[i][j] = dpmin[i][j - 1];//注意,保存的是深度最小值的下标
53             else dpmin[i][j] = dpmin[i + (1 << (j - 1))][j - 1];
54         }
55 }
56 int work(int a, int b)//基本的rmq的算法
57 {
58     int l = pos[a], r1 = pos[b];
59     if(l > r1)swap(l, r1);
60     int k = (int)(log((double)(r1 - l + 1)) / log(2.0));
61     if(depth[dpmin[l][k]] < depth[dpmin[r1 - (1 << k) + 1][k]]) return r[dpmin[l][k]];
62     else return r[dpmin[r1 - (1 << k) + 1][k]];
63 }
64 int main()
65 {
66     scanf("%d", &T);
67     while(T--)
68     {
69         init();
70         scanf("%d", &n);
71         for(int i = 1; i < n; i++)
72         {
73             int u, v;
74             scanf("%d%d", &u, &v);
75             e[u].push_back(v);//存入边
76             ind[v]++;
77         }
78         for(int i = 1; i <= n; i++)
79         if(!ind[i]){
80             root = i;break;//入度为0就是根
81         }
82         dfs(root,0);//dfs一边
83         RMQ();
84         int a, b;
85         scanf("%d%d", &a, &b);
86         printf("%d\n", work(a,b));
87     }
88     return 0;
89 }
View Code

 

转载于:https://www.cnblogs.com/kiritoghy/p/4689037.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值