Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA模版
来源:互联网 发布:紧急域名升级通知 编辑:程序博客网 时间:2024/06/06 09:07
题目链接: Misha, Grisha and Underground
题目大意
给你一颗无根树
三个顶点f, s, t, 计算f到s的最短路径和f到t的最短路径的公共顶点个数
给你三个点a, b, c, 求如何将a, b, c对应到f, s, t, 使得上面描述的公共顶点个数最多, 输出最多的公共顶点个数
思路
将顶点1定义为树根, 求出所有顶点到树根的最短路长度depth[i]
那么两个顶点u, v的最短路长度为dis(u, v) = depth[u] + depth[v] - depth[lca(u, v)]*2
, lca(u, v)为uv的最近公共祖先
那么f到s和f到t的最短路径的公共边数量就等于(dis(f, s)+dis(f, t)-dis(s, t))/2
公共顶点数量还要再+1
代码
第一份代码用的基于二分的LCA,
第二份代码用的是基于RMQ(ST表)的LCA,
基于二分的LCA
GNU C++14 Accepted 327 ms 18100 KB
#include <bits/stdc++.h>using namespace std;const int maxv = 1e5 + 100, maxlog = 17;vector<int> G[maxv];int root, par[maxlog][maxv];int depth[maxv];//预处理O(nlogn), 查询log(n)void dfs(int v, int p, int d){ par[0][v] = p; depth[v] = d; for(auto &ite : G[v]) if(ite!=p) dfs(ite, v, d+1);}void init(int V){ dfs(root, -1, 0); for(int k=0; k+1<maxlog; ++k) { for(int v=1; v<=V; ++v) { if(par[k][v] < 0) par[k+1][v] = -1; else par[k+1][v] = par[k][par[k][v]]; } }}int lca(int u, int v){ if(depth[u] > depth[v]) swap(u, v); for(int k=0; k<maxlog; ++k) { if(((depth[v]-depth[u]) >> k) & 1) v = par[k][v]; } if(u == v) return u; for(int k=maxlog-1; k>=0; --k) { if(par[k][u] != par[k][v]) { u = par[k][u]; v = par[k][v]; } } return par[0][u];}void add(int u, int v){ G[u].push_back(v); G[v].push_back(u);}const int maxn = 1e5 + 10;int n, q, u, v;int dis(int u, int v){ return depth[u] + depth[v] - depth[lca(u, v)]*2;}int cal(int f, int s, int t){ return (dis(f, s)+dis(f, t)-dis(s, t))/2;}int main(){ scanf("%d%d", &n, &q); for(int i=2; i<=n; ++i) { scanf("%d", &u); add(u, i); } root = 1; init(n); int a, b, c; while(q--) { scanf("%d%d%d", &a, &b, &c); printf("%d\n", max(cal(a, b, c), max(cal(b, a, c), cal(c, a, b))) + 1); } return 0;}
基于RMQ的LCA
GNU C++14 Accepted 264 ms 36500 KB
#include <bits/stdc++.h>using namespace std;const int maxv = 1e5+100;vector<int> G[maxv];int root, vs[maxv*2], depth[maxv*2], id[maxv], mi[maxv*2][30];void init_rmq(int n){ int a, b; for(int i=1; i<=n; ++i) mi[i][0] = i; for(int j=1; (1<<j)<=n; ++j) { for(int i=1; i+(1<<j)-1<=n; ++i) { a = mi[i][j-1], b = mi[i+(1<<(j-1))][j-1]; mi[i][j] = depth[a]<depth[b] ? a : b; } }}int rmq(int l, int r){ int k = 0; while((1<<(k+1)) <= r-l+1) ++k; int a = mi[l][k], b = mi[r-(1<<k)+1][k]; return depth[a] < depth[b] ? a : b;}void dfs(int v, int p, int d, int &k){ id[v] = k; vs[k] = v; depth[k++] = d; for(auto &ite : G[v]) { if(ite!=p) { dfs(ite, v, d+1, k); vs[k] = v; depth[k++] = d; } }}void init(int V){ int k = 1; dfs(root, -1, 0, k); init_rmq(V*2-1);}int lca(int u, int v){ if(id[u] > id[v]) swap(u, v); return vs[rmq(id[u], id[v])];}void add(int u, int v){ G[u].push_back(v); G[v].push_back(u);}const int maxn = 1e5 + 10;int n, q, u, v;int dis(int u, int v){ return depth[id[u]] + depth[id[v]] - depth[id[lca(u, v)]]*2;}int cal(int f, int s, int t){ return (dis(f, s)+dis(f, t)-dis(s, t))/2;}int main(){ scanf("%d%d", &n, &q); for(int i=2; i<=n; ++i) { scanf("%d", &u); add(u, i); } root = 1; init(n); int a, b, c; while(q--) { scanf("%d%d%d", &a, &b, &c); printf("%d\n", max(cal(a, b, c), max(cal(b, a, c), cal(c, a, b))) + 1); } return 0;}
阅读全文
0 0
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA模版
- Codeforces Round #425 (Div. 2)D. Misha, Grisha and Underground
- Codeforces Round #425 (Div. 2) D.Misha, Grisha and Underground
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA
- Codeforces Round #425 (Div. 2)D. Misha, Grisha and Underground(LCA)
- codeforces D. Misha, Grisha and Underground(LCA)
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground 最近公共祖先
- CodeForces 832D Round #425 D Misha, Grisha and Underground :LCA求树上路径长度
- 【Lca 倍增】codeforces 832D Misha, Grisha and Underground
- Codeforces 832D-Misha, Grisha and Underground(LCA)
- Codeforces 832D. Misha, Grisha and Underground【LCA】
- Codeforces 832D Misha, Grisha and Underground【LCA】
- codeforces 832D Misha, Grisha and Underground 倍增lca
- CF #425 DIV2 D. Misha, Grisha and Underground(LCA)
- codeforces 832D Misha, Grisha and Underground
- Codeforces 832D Misha, Grisha and Underground
- [codeforces 832D]Misha, Grisha and Underground
- Jsonp原理
- AI智能人机对战五子棋(Java实现图形界面)
- Springboot-session
- JavaScript数据结构(2):栈与队列
- Dijkstra(迪杰斯特拉) 最短路+模板
- Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground LCA模版
- iOS10App如何跳转到系统设置
- 使用LinkItOne板子遇到的问题
- BUAA OJ 382 中世界的Thor
- 寻找大富翁
- python正则表达式,向前向后查找与回溯引用
- 几种排序方法
- vue.js开发神器devtools的安装方法
- 线性表 —— 2