Ural1471-Distance in the Tree
来源:互联网 发布:全球使命登陆网络断开 编辑:程序博客网 时间:2024/05/20 19:49
大致题意
给定一棵树,求两个节点之间的距离。
解题思路
以0为根节点,先dfs求出遍历的节点顺序和对应的节点深度,记录每个节点在欧拉序列中第一次出现的下标,以及每个节点到根节点的距离。
预处理RMQ能够快速得到在欧拉序列中的某个区间中离根最近的点(深度最浅),然后通过寻找两个节点u,v在欧拉序列中首次出现的位置区间内深度最浅的对应节点就是LCA(u,v),则u,v之间的距离为dis[u]-dis[t]+dis[v]-dis[t]。
- 代码
#include <cstdio>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int maxn = 50000 + 5;const int max_logn = 18;struct edge { int to, weight;};vector<edge> G[maxn];int vs[maxn*2-1]; // 欧拉序列int id[maxn]; // 节点在欧拉序列中首次出现的位置int depth[maxn*2-1]; // 节点在欧拉序列中节点的深度int dis[maxn]; // 节点到跟节点的距离int st[maxn*2-1][max_logn];int n, root;void dfs(int v, int p, int d, int w, int &k) { id[v] = k; vs[k] = v; depth[k++] = d; dis[v] = w; for (int i = 0; i < G[v].size(); i++) { if (G[v][i].to != p) { dfs(G[v][i].to, v, d + 1, w + G[v][i].weight, k); vs[k] = v; depth[k++] = d; } }}//[l, r)void rmq_init(int n) { for (int i = 0; i < n; i++) { st[i][0] = i; } for (int j = 1; (1 << j) < n; j++) { for (int i = 0; i + (1 << j) - 1 < n; i++) { if (depth[st[i][j-1]] <= depth[st[i+(1<<(j-1))][j-1]]) { st[i][j] = st[i][j-1]; } else { st[i][j] = st[i+(1<<(j-1))][j-1]; } } }}void init() { int k = 0; dfs(root, -1, 0, 0, k); rmq_init(n * 2 - 1);}int query(int L, int R) { int k = log2(R - L); if (depth[st[L][k]] <= depth[st[R-(1<<k)][k]]) { return st[L][k]; } else { return st[R-(1<<k)][k]; }}int lca(int u, int v) { return vs[query(min(id[u], id[v]), max(id[u], id[v])+1)];}int main(int argc, char const *argv[]) { scanf("%d", &n); root = 0; for (int i = 0; i < n - 1; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].push_back((edge){v, w}); G[v].push_back((edge){u, w}); } init(); int m; scanf("%d", &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); int t = lca(u, v); int len = dis[u] - dis[t] + dis[v] - dis[t]; printf("%d\n", len); } return 0;}
0 0
- Ural1471-Distance in the Tree
- Distance in Tree
- codeforces D. Distance in Tree
- The furthest distance in the world
- The Farthest Distance in the World
- The furthest distance in the world
- The Farthest Distance in the World
- the furthest distance in the world
- The furthest distance in the world
- the furthest distance in the world
- The Farthest Distance in the World (Tagore)
- CF-161D Distance in Tree
- Codeforces 161D Distance in Tree
- Codeforces 161D Distance in Tree
- the longest distance of a binary tree
- 2012-03-29 The furthest distance in the world
- codeforces 161D Distance in Tree(树形dp)
- CodeForces 160D - Distance in Tree 树形DP
- 【JS学习】——基础知识
- 动态规划-免费馅饼
- 字符集和字符编码
- 计数器 FPGA 电路实验 作业
- Unity5.x制作RPG游戏
- Ural1471-Distance in the Tree
- 关于Studio sdk 设置的问题
- iOS开发之----动态类型(isKindOfClass, isMemberOfClass,id)等
- 13 插入数据
- Eclipse 乱码 解决方案总结(UTF8 -- GBK)
- mysql 插入相同主键的解决方案
- 以太坊《私有链和联盟链的机会与挑战》报告
- Objective-C多态
- 软件需求及分析