题干:

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). ttest 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 ab 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:
16
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONEOutput:
5
3

解题报告:

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 20000 + 5;
int dep[MAX],fa[MAX][33],cost[MAX][33];int n,m;
vector<int> vv[MAX];
vector<int> ww[MAX];
void dfs(int cur, int rt) {fa[cur][0] = rt;dep[cur] = dep[rt] + 1;for(int i = 1; i < 31; ++i) {fa[cur][i] = fa[fa[cur][i - 1]][i - 1];cost[cur][i] = cost[fa[cur][i - 1]][i - 1] + cost[cur][i - 1];}int sz = vv[cur].size();for (int i = 0; i < sz; ++i) {if (vv[cur][i] == rt) continue;cost[vv[cur][i]][0] = ww[cur][i];dfs(vv[cur][i], cur);}
}
int lca(int u,int v) {if(dep[u] < dep[v]) swap(u,v);int res = 0;int dc = dep[u]-dep[v];for(int i = 0; i<=30; i++) {if((1<<i) & dc) res += cost[u][i],u=fa[u][i];}if(u == v) return res;for(int i = 30; i>=0 && u!=v; i--) {if(fa[u][i] != fa[v][i]) {res += cost[v][i] + cost[u][i];u=fa[u][i];v=fa[v][i];}}res += cost[u][0] + cost[v][0];u=fa[u][0];return res;
}
int fk(int a,int b,int k) {int u=a,v=b;if(dep[u] < dep[v]) swap(u,v);int dc = dep[u]-dep[v];for(int i = 0; i<=30; i++) {if((1<<i) & dc) u=fa[u][i];}if(u!=v) {for(int i = 30; i>=0 && u!=v; i--) {if(fa[u][i] != fa[v][i]) {u=fa[u][i];v=fa[v][i];}}u=fa[u][0];//得到公共祖先}int cur = 0;int ans = 0;if(dep[a] - dep[u] >= k) {int RES = k-1;if(RES!=0) {for(int i = 30; i>=0; i--) {int now = fa[a][i];if(dep[a] - dep[now] < RES) {int tmp = dep[a] - dep[now];a = fa[a][i];RES -= tmp;}}ans = fa[a][0];} else ans = a; } else if(dep[a] - dep[u] == k-1) {ans = u;} else {int RES = k - (dep[a] - dep[u])-1;RES = (dep[b]-dep[u]-RES);if(RES != 0) {for(int i = 30; i>=0; i--) {int now = fa[b][i];if(dep[b] - dep[now] < RES) {int tmp = dep[b] - dep[now];b = fa[b][i];RES -= tmp;}}ans = fa[b][0];}else ans = b;}return ans;
}
int main() {int t;cin>>t;while(t--) {scanf("%d",&n);memset(dep,0,sizeof dep);memset(fa,0,sizeof fa);memset(cost,0,sizeof cost);for(int i = 1; i<=n; i++) vv[i].clear(),ww[i].clear();for(int a,b,c,i = 1; i<=n-1; i++) {scanf("%d%d%d",&a,&b,&c);vv[a].pb(b);vv[b].pb(a);ww[a].pb(c);ww[b].pb(c);}dfs(1,-1);char op[22];while(scanf("%s",op)) {int a,b,k;if(!strcmp(op,"DIST")) {scanf("%d%d",&a,&b);printf("%d\n",lca(a,b));} else if(!strcmp(op,"KTH")) {scanf("%d%d%d",&a,&b,&k);printf("%d\n",fk(a,b,k));} else break;}if(t) puts("");}return 0 ;
}

【SPOJ - QTREE2】Query on a tree II(LCA,倍增)相关推荐

  1. SPOJ - QTREE2 Query on a tree II(LCA)

    题目链接:点击查看 题目大意:给出一棵无根树,每条边都有权值,接下来有数次操作,每次操作分为两种类型: DIST x y:求出点x到点y的唯一路径上的权值和 KTH x y k:求出点x到点y的唯一路 ...

  2. LCA SP913 QTREE2 - Query on a tree II

    SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点 ...

  3. SP913 QTREE2 - Query on a tree II

    思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...

  4. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

  5. SPOJ 375. Query on a tree (树链剖分)

    题目链接: http://www.spoj.com/problems/QTREE/ 375. Query on a tree Problem code: QTREE You are given a t ...

  6. spoj 375 Query on a tree (树链剖分)

    题目链接: http://www.spoj.com/problems/QTREE/ 题意: 给一颗树,每条边有一个权值.有两种操作: 1.修改某条边的值: 2.询问a.b两点路径上边权的最大值. 分析 ...

  7. SPOJ 375 Query on a tree(线段树维护树链剖分)

    题目链接:http://www.spoj.com/problems/QTREE/ 题意:给出一个树,两种操作:(1)修改某条边的权值:(2)询问某两个顶点之间边的最大值. 思路:树的路径剖分和线段树维 ...

  8. SPOJ - QTREE Query on a tree(树链剖分+线段树)

    题目链接:点击查看 题目大意:给出一棵由n个点组成的树,再给出数个操作,每次操作分为下列几种类型: QUERY x y:询问点x-点y这条路径上的所有边权的最大值 CHANGE x y:将第x条边的权 ...

  9. spoj 375 Query on a tree

    题意:给一棵树,节点数不超过10000,有两个操作:1.询问a,b路径上最长的边长.2.把第a条边长度改为b. p.s.人生中第一个树链剖分,尼玛debug了好久好久我擦... 分析:轻重边路径剖分, ...

最新文章

  1. php strace 工具,Linux程序调试工具工具—strace命令
  2. openstack——horizon篇
  3. 不要将时间浪费到编写完美代码上
  4. java 打开jsp文件_jsp文件怎么打开(java-web中jsp的理解)
  5. java程序编写九九乘法表_用面向对象的方法编写的九九乘法表java代码的编写
  6. Nova Suspend/Rescue 操作详解 - 每天5分钟玩转 OpenStack(35)
  7. DOM包裹wrap()方法
  8. 腾讯微博——点击按钮自动加关注代码
  9. 【思科百难】RIP两个版本之间能够相互通信?
  10. TurtleCoin节点搭建
  11. cli vue webpack 实战_Vuejs技术栈从CLI到打包上线实战全解析
  12. 华为百度美团驰援抗击疫情;自由软件基金会建议开源 Windows 7;印度超越美国成第二大智能手机市场 | 极客头条...
  13. 探究requestDisallowInterceptTouchEvent失效的原因
  14. SQLServer中替换某字段的部分内容
  15. 制作风格——百变幻灯片,完全DIY(高级教程)
  16. mysql asc_mysql – 在字符串列上使用asc和desc的索引
  17. python练习实例——字母图形
  18. EPICS IOC Shell
  19. 系统架构设计师论文历年考题(2015-2017)考前冲刺来一波真题
  20. FMM 大战 LMM - SOFR 企稳 Part I

热门文章

  1. nlp中的经典深度学习模型(二)
  2. 753 Cracking the Safe
  3. [签名算法]DSA 算法
  4. [剑指offer][JAVA][面试题56 - I][第260题][位运算][HashSet]
  5. Codeforces Round #413 C-Fountains 树状数组
  6. java get与post区别_HTTP请求(GET与POST区别)和响应
  7. 福师2018计算机应用基础,中石油华东《计算机应用基础》2018年秋学期在线作业100分答案满分...
  8. php访问mysql函数吗,PHP访问MySQL数据库函数简介
  9. js urlencode 20 php,js实现php函数urlencode
  10. python杀死了excel_Python杀死了Excel|自动更新表格,告别繁琐