Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Description

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 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:
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

Hint

Added by: Thanh-Vy Hua
Date: 2006-08-27
Time limit: 0.433s
Source limit: 15000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: ERL JS NODEJS PERL 6 VB.net
Resource: Special thanks to Ivan Krasilnikov for his alternative solution

有两种操作,一是求两点间距离,二是求一点到另一点路径上的第k个点。

LCA妥妥的。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,dis;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=d;hd[u]=mct;return;
}
int T,n;
int fa[mxn][];
int dep[mxn];
int dis[mxn];
void init(){memset(hd,,sizeof hd);memset(fa,,sizeof fa);mct=;}
void DFS(int u,int f){
dep[u]=dep[f]+;
for(int i=;i<;i++)fa[u][i]=fa[fa[u][i-]][i-];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
fa[v][]=u;
dis[v]=dis[u]+e[i].dis;
DFS(v,u);
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=;i>=;i--)
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return y;
for(int i=;i>=;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][];
}
inline int dist(int x,int y){//求距离
int tmp=LCA(x,y);
return dis[x]+dis[y]-dis[tmp]*;
}
inline int find(int x,int k){//上溯
for(int i=;i>=;i--){
if(k&(<<i))x=fa[x][i];
}
return x;
}
inline int solve(int x,int y,int k){//查询从x到y路径上第k个结点
int tmp=LCA(x,y);
int mid=dep[x]-dep[tmp]+;
if(k==mid)return tmp;
if(k>mid){
int dd=dep[y]-dep[tmp]+;
mid=k-mid+;
k=dd-mid;
return find(y,k);
}
else
return find(x,k-);
}
int main(){
T=read();
int i,j,x,y,d;
while(T--){
init();
n=read();
for(i=;i<n;i++){
x=read();y=read();d=read();
add_edge(x,y,d);
add_edge(y,x,d);
}
int rt=n/+;
dis[rt]=;
DFS(rt,);
char op[];
while(scanf("%s",op) && (op[]!='D' || op[]!='O')){
if(op[]=='K'){
x=read();y=read();d=read();
printf("%d\n",solve(x,y,d));
}
if(op[]=='D'){
x=read();y=read();
printf("%d\n",dist(x,y));
}
}
}
return ;
}

SPOJ913 Query on a tree II的更多相关文章

  1. 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至点 ...

  2. 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 ...

  3. [SPOJ913]QTREE2 - Query on a tree II【倍增LCA】

    题目描述 [传送门] 题目大意 给一棵树,有两种操作: 求(u,v)路径的距离. 求以u为起点,v为终点的第k的节点. 分析 比较简单的倍增LCA模板题. 首先对于第一问,我们只需要预处理出根节点到各 ...

  4. 【SPOJ QTREE2】QTREE2 - Query on a tree II(LCA)

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  5. Query on a tree II 倍增LCA

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  6. LCA【SP913】Qtree - Query on a tree II

    Description 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点b有向路径上的第k个点的编号 有多组测试数据 ...

  7. SPOJ Query on a tree II (树剖||倍增LCA)(占位)

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  8. SPOJ 913 Query on a tree II

    spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B ...

  9. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

    QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...

随机推荐

  1. Python2.4-原理之函数

    此节来自于<Python学习手册第四版>第四部分 一.函数基础 函数的作用在每个编程语言中都是大同小异的,,这个表是函数的相关语句和表达式. 1.编写函数,a.def是可执行代码,pyth ...

  2. sql server 创建只读帐号

    有时候为了方便查询一下数据,会创建个只读帐号,以免误写sql语句改了数据 步骤:用sa帐号连接后,安全性--登录名--新建 输入要新建的帐号密码,在服务器角色里面单勾一个public 在 用户映射里面 ...

  3. java并发:线程同步机制之Volatile关键字&原子操作Atomic

    volatile关键字 volatile是一个特殊的修饰符,只有成员变量才能使用它,与Synchronized及ReentrantLock等提供的互斥相比,Synchronized保证了Synchro ...

  4. iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...

  5. 如何解决Windows 10系统下设备的声音问题

    如何解决Windows 10系统下设备的声音问题? 请阅读下面的说明来解决Windows 10设备上的声音问题. 1. 检查设备管理器 打开开始菜单,键入设备管理器, 从出现的结果中选择并打开它. 在 ...

  6. Centos下编译JDK

    因为OpenJDK是开源的,这里使用openJDK进行编译联系 环境要求 Centos6.7 64位 openjdk-7u40-fcs-src-b43-26_aug_2013.zip bootstra ...

  7. linux基础-第十三单元 硬盘分区、格式化及文件系统的管理二

    第十三单元 硬盘分区.格式化及文件系统的管理二 文件系统的挂载与卸载 什么是挂载 mount命令的功能 mount命令的用法举例 umount命令的功能 umount命令的用法举例 利用/etc/fs ...

  8. C#分割字符串

    命名空间:System.String.Split 程序集:mscorlib( mscorlib.dll) 简单实例: string before = "12,50,30"; str ...

  9. Beta版本冲刺———第二天

    会议照片: 项目燃尽图: 1.项目进展: 昨天的困难:分数排行榜的设计 今天解决的进度:完成了界面优化以及建立新的排行榜选项卡界面. 明天要做的事情:分数排行榜的功能设计 2.每个人每天做的事情 郭怡 ...

  10. lineNumber: 1; columnNumber: 1; 前言中不允许有内容

    周六项目还能运行,也没修改什么,周一来了启动项目,竟然报错了~~~ 这是遇到的错误提示: Cause: org.xml.sax.SAXParseException; lineNumber: 1; co ...