博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【lca】lca转rmq写法 poj1330
阅读量:6035 次
发布时间:2019-06-20

本文共 4764 字,大约阅读时间需要 15 分钟。

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
题目链接:
                                                                                       
Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

 

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

 代码:

1 #include
2 #include
3 #include
4 #include
5 #include
6 #define mem(a,x) memset(a, x, sizeof(a)) 7 using namespace std; 8 9 vector
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

你可能感兴趣的文章
Layout父元素点击不到的解决办法
查看>>
【面试次体验】堆糖前端开发实习生
查看>>
基于apache实现负载均衡调度请求至后端tomcat服务器集群的实现
查看>>
C#+QQEmail自动发送邮件
查看>>
[Hadoop]MapReduce多输出
查看>>
Android Activity详解(一)
查看>>
快准车服完成3000万元A+轮融资,年底将开始B轮融资
查看>>
让我去健身的不是漂亮小姐姐,居然是贝叶斯统计!
查看>>
MySQL 数据约束
查看>>
我的友情链接
查看>>
SERVLET容器简介与JSP的关系
查看>>
《服务器SSH Public Key认证指南》-补充
查看>>
我的友情链接
查看>>
Java break continue return 的区别
查看>>
算法(Algorithms)第4版 练习 1.3.4
查看>>
jquery easyUI checkbox复选项获取并传后台
查看>>
浅析NopCommerce的多语言方案
查看>>
设计模式之简单工厂模式
查看>>
C++中变量的持续性、链接性和作用域详解
查看>>
2017 4月5日上午
查看>>