博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LCA - 倍增法去求第几个节点
阅读量:5837 次
发布时间:2019-06-18

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

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.

We will ask you to perfrom some instructions of the following form:

  • DIST a b : ask for the distance between node a and node b
    or
  • KTH a b k : ask for the k-th node on the path from node a to node b

Example:

N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2
Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)

Input

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000)
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
  • The next lines contain instructions "DIST a b" or "KTH a b k"
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "DIST" or "KTH" operation, write one integer representing its result.

Print one blank line after each test.

Example

Input:161 2 12 4 12 5 21 3 13 6 2DIST 4 6KTH 4 6 4DONEOutput:53 题目分析 : 两种操作,一是查看任意两点间的距离,二是查询从a到b第k个点是什么 思路分析 : 裸的 LCA ,对于第二种操作,由于倍增中存的就是跳跃的点,所以查看跳到的第几个点也是很容易的 代码示例 :
#define ll long longconst ll maxn = 1e4+5;const double pi = acos(-1.0);const ll inf = 0x3f3f3f3f;struct node{    ll to, cost;    node(ll _to = 0, ll _cost = 0):to(_to), cost(_cost){}};vector
ve[maxn];ll dep[maxn];ll grand[maxn][20], gw[maxn][20];ll N;void dfs(ll x, ll fa){ for(ll i = 1; i <= N; i++){ grand[x][i] = grand[grand[x][i-1]][i-1]; gw[x][i] = gw[x][i-1] + gw[grand[x][i-1]][i-1]; } for(ll i = 0; i < ve[x].size(); i++){ ll to = ve[x][i].to; ll cost = ve[x][i].cost; if (to == fa) continue; grand[to][0] = x; gw[to][0] = cost; dep[to] = dep[x] + 1; dfs(to, x); }}ll ans = 0;ll a, b, c;ll lca(){ if (dep[a] > dep[b]) swap(a, b); ans = 0; for(ll i = N; i>= 0; i--){ if (dep[a] < dep[b] && dep[grand[b][i]] >= dep[a]) { ans += gw[b][i]; b = grand[b][i]; } } for(ll i = N; i >= 0; i--){ if (grand[a][i] != grand[b][i]) { ans += gw[a][i]; ans += gw[b][i]; a = grand[a][i], b = grand[b][i]; } } if (a != b) return grand[a][0]; else return a;}ll fid(ll a, ll b, ll f) { if (dep[a]-dep[f]+1 >= c){ ll len = dep[a] - c + 1; if (len == 0) return 1; for(ll i = N; i >= 0; i--){ if (dep[grand[a][i]] >= len){ a = grand[a][i]; } } return a; } else { ll len = c - (dep[a]-dep[f]+1) + dep[f]; for(ll i = N; i >= 0; i--){ if (dep[grand[b][i]] >= len){ b = grand[b][i]; } } return b; }}int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); ll t, n; char s[20]; ll p1, p2; cin >> t; while(t--){ scanf("%lld", &n); for(ll i = 1; i <= 10000; i++) ve[i].clear(); for(ll i = 1; i< n; i++){ scanf("%lld%lld%lld", &a, &b, &c); ve[a].push_back(node(b, c)); ve[b].push_back(node(a, c)); } N = floor(log(n)/log(2)); memset(grand, 0, sizeof(grand)); memset(gw, 0, sizeof(gw)); dep[1] = 0; dfs(1, 1); while(1){ scanf("%s", s); if (s[1] == 'O') break; else if (s[1] == 'I') { scanf("%lld%lld", &a, &b); ll f = lca(); if (a != b) { ans += gw[a][0]; ans += gw[b][0]; } printf("%lld\n", ans); } else if (s[1] == 'T'){ scanf("%lld%lld%lld", &a, &b, &c); p1 = a, p2 = b; ll f = lca(); printf("%lld\n", fid(p1, p2, f)); } } } return 0;}/*1081 2 11 3 21 7 32 4 42 5 53 6 67 8 7KTH 4 8 3DIST*/

 

转载于:https://www.cnblogs.com/ccut-ry/p/8426748.html

你可能感兴趣的文章
面试题:猫叫、老鼠跑、人醒的一点看法
查看>>
c#操作access,update语句不执行的解决办法
查看>>
Position 5: Architect of Linux BSP
查看>>
关于std::string
查看>>
AI 高等数学、概率论基础
查看>>
react做股票、期货交易遇到的问题(不完全是react)及解决方法。
查看>>
POJ1351 Number of Locks(DP:记忆化搜索)
查看>>
vue-router的beforeEach的使用?
查看>>
C程序设计语言 导言
查看>>
ibatis 的使用
查看>>
POJ 3253:Fence Repair
查看>>
03-18 js作业整理
查看>>
java例程练习(数三退一[用数组模拟])
查看>>
无刷新分页前端代码
查看>>
只显示指定网卡IP地址命令
查看>>
VS单元测试的一些常见问题和解决办法
查看>>
wordvec_词的相似度
查看>>
Centos6.5使用yum安装mysql——快速上手必备
查看>>
java常用注解
查看>>
java项目开发第六天——天若有情天亦老,人间正道是沧桑
查看>>