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:
1 6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE Output:
5
3

先以一点为根做dfs,计算每个点的深度、父亲、离根的距离,倍增法找出LCA,两点的距离就能算出来了。

路径上的第K个值则先判断LCA到起点的深度差是否大于k,是则在起点到LCA的路径上,否则在LCA到终点的路径上。

#include <iostream>
#include <cstdio>
#include <cstring>
#define N 10005
using namespace std;
struct edge{
int to,next,w;
}e[N<<];
int head[N],cnt;
void add(int u,int v,int w){
e[++cnt]=(edge){v,head[u],w};
head[u]=cnt;
}
int n;
int deep[N],dis[N],p[N][];
void dfs(int x,int fa){
deep[x]=deep[fa]+;
p[x][]=fa;
for(int i=;p[x][i];i++)
p[x][i+]=p[p[x][i]][i];//由x的2^i祖先和祖先的2^i祖先算出x的2^(i+1)祖先
for(int i=head[x];i;i=e[i].next){
int to=e[i].to;
if(to==fa)continue;
dis[to]=dis[x]+e[i].w;
dfs(to,x);
}
}
int lca(int a,int b){
if(deep[a]<deep[b])swap(a,b);
for(int j=;j>=;j--)
if(deep[a]-(<<j)>=deep[b])//将a上移到和b深度一样
a=p[a][j];
if(a==b)return a;
for(int j=;j>=;j--)
if(p[a][j]&&p[a][j]!=p[b][j]){//a、b一起上移
a=p[a][j];
b=p[b][j];
}
return p[a][];
}
int get(int u,int d){//u上升到d深度
for(int j=;j>=;j--)
if(deep[u]-(<<j)>=d)
u=p[u][j];
return u;
}
int main() {
int t;
cin>>t;
while(t--){
memset(head,,sizeof head);
memset(p,,sizeof p);//这个要清空!!
cnt=;
cin>>n;
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
//deep[0]=0;dis[1]=0;这可以不写
dfs(,);
char op[];
while(){
scanf("%s",op);
if(op[]=='O')break;
if(op[]=='I'){
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",dis[u]+dis[v]-*dis[lca(u,v)]);
}else{
int u,v,k;
scanf("%d%d%d",&u,&v,&k);
int fa=lca(u,v),d;
if(deep[u]-deep[fa]<k){
d=deep[fa]+k-(deep[u]-deep[fa]+);
u=v;
}
else d=deep[u]-k+;//需要的深度
printf("%d\n",get(u,d));
}
}
}
}
  

【SPOJ QTREE2】QTREE2 - Query on a tree II(LCA)的更多相关文章

  1. 【SPOJ - GSS2】Can you answer these queries II(线段树)

    区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include< ...

  2. 【HANA系列】SAP 【第一篇】EXCEL连接SAP HANA的方法(ODBC)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP [第一篇]EXCEL连接 ...

  3. 【学习笔记】深入理解js原型和闭包(0)——目录

    文章转载:https://www.cnblogs.com/wangfupeng1988/p/4001284.html 说明: 本篇文章一共16篇章,外加两篇后补的和一篇自己后来添加的学习笔记,一共19 ...

  4. 【HANA系列】SAP 【第二篇】EXCEL连接SAP HANA的方法(ODBC)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP [第二篇]EXCEL连接 ...

  5. 【持久化框架】Mybatis与Hibernate的详细对比(转发)

    前言 这篇博文我们重点分析一下Mybatis与Hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...

  6. spoj 913 Query on a tree II (倍增lca)

    Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...

  7. 【Python实战】模块和包导入详解(import)

    1.模块(module) 1.1 模块定义 通常模块为一个.py文件,其他可作为module的文件类型还有".pyo".".pyc".".pyd&qu ...

  8. 【转帖】HBase读写的几种方式(二)spark篇

    HBase读写的几种方式(二)spark篇 https://www.cnblogs.com/swordfall/p/10517177.html 分类: HBase undefined 1. HBase ...

  9. 【Amaple教程】3. 模板指令与状态数据(state)

    一个模块的template模板.JavaScript和css之间的关系其实可以如下图表示: 如果你了解Angular.Vue动态模板,那你将会对Amaple的模板感到很熟悉,在Amaple中,temp ...

随机推荐

  1. [No000044]你是否还傻到把最好的留在最后?

    想写这篇文章很久了. 因为一直觉得我们人有一个毛病,总是喜欢将最好的东西留到最后才享用,或者等最后再给别人. 但人们往往忽略了至关重要的一点,就是这个最好有一个保质期.可以说人能拥有的快乐触发点.有机 ...

  2. 南邮CTF隐写之丘比龙的女神

    刚开始下载下图片来 习惯性的binwalk一下 没发现东西 formost一下也没分离出来 扔进c32asm中发现有nvshen.jpg 于是改后缀名字为.zip 解压nvshen.jpg发现无法解压 ...

  3. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  4. Samsung I9103刷cm-10.1的方法

    按照官方网站的说明一步一步的做下去的时候发现在执行heimdall.exe文件的时候出现“不是win32的应用程序”的错误提示,因此决定按照其它方法安装recovery,然后再刷入CM10.1. sa ...

  5. 为什么做前端要做好SEO

    我就挑干货说啦SEO可能听起来很高大上,其实翻译成中文就是"搜索引擎优化",它只是通过一定的方法在网站内外发布文章.交换连接等,最终达到某个关键词在搜索引擎上获得好的排名. 我有幸 ...

  6. wireshake抓包,飞秋发送信息,python

    http://wenku.baidu.com/link?url=Xze_JY8T15pqI9mBLRpTxWF2d6MP-32xb6UwuE6tsUmitRDheJe-Ju87WlDEDBGuI5MF ...

  7. iOS多线程开发

    概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操 ...

  8. 如何在Eclipse和Tomcat的Debug过程中启用热部署

    参考的地址是 http://blog.redfin.com/devblog/2009/09/how_to_set_up_hot_code_replacement_with_tomcat_and_ecl ...

  9. IT教程网

    这个IT教程网(印度),我认为是最好的.里面的知识基础实用,覆盖面很广,作为IT入门和了解都是极好的. http://www.tutorialspoint.com/

  10. 如何把Json格式字符写进text文件中

    本篇一步一步学习怎样把显示于网页的json格式的字符串写进text文件中,并保存起来.学习到创建model, Entity, 序列化List<object>转换为json,显示于网页上.然 ...