Query on a tree II

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e4+;
const int M=N*N+;
int n,m,k,tot=;
int fa[*N][],head[N*],dis[N*],dep[N*];
struct man{
int to,next,w;
}edg[*N];
void add(int u,int v,int w){
edg[tot].to=v;edg[tot].next=head[u];edg[tot].w=w;head[u]=tot++;
}
void init(){
met(head,-);met(fa,);met(dis,);met(dep,);
tot=;
}
void dfs(int u,int f){
fa[u][]=f;
for(int i=;i<;i++){
fa[u][i]=fa[fa[u][i-]][i-];
}
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=f){
dis[v]=dis[u]+edg[i].w;
dep[v]=dep[u]+;
dfs(v,u);
}
}
}
int LCA(int u,int v){
int U=u,V=v;
if(dep[u]<dep[v])swap(u,v);
for(int i=;i>=;i--){
if(dep[fa[u][i]]>=dep[v]){
u=fa[u][i];
}
}
if(u==v)return (u);
for(int i=;i>=;i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];v=fa[v][i];
}
}
return (fa[u][]);
}
int find_kth(int u,int v,int lca,int k){
k--;
if(dep[u]-dep[lca]<k){
k=dep[u]-dep[lca]*+dep[v]-k;
u=v;
}
for(int i=;i<;i++){
if(k&(<<i)){ //注意这里
u=fa[u][i];
}
}
return u;
}
void solve(){
int u,v,val;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&val);
add(u,v,val);add(v,u,val);
}
dep[]=;
dfs(,);
char str[];
while(){
scanf("%s",str);
if(str[]=='D'&&str[]=='I'){
scanf("%d%d",&u,&v);
int lca=LCA(u,v);
printf("%d\n",dis[u]+dis[v]-*dis[lca]);
}
else if(str[]=='K'){
scanf("%d%d%d",&u,&v,&k);
int lca=LCA(u,v);
printf("%d\n",find_kth(u,v,lca,k));
}
else break;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
init();
solve();
}
return ;
}

spoj 913 Query on a tree II (倍增lca)的更多相关文章

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

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

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

  3. Query on a tree II 倍增LCA

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

  4. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  5. 【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, ...

  6. SPOJ QTREE2 Query on a tree II

    传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #defi ...

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

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

  8. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

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

随机推荐

  1. Linux认知之旅【06 图形界面上的各种折腾】!

    玩linux免不了折腾,不折腾对不起linux 初次接触, 总会接触到绚丽的linux桌面! 但是随之而来的桌面优化,字体安装,操作习惯都需要一一适应

  2. Python全栈 MySQL 数据库 (表字段增、删、改、查、函数)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图         查询SQL变量 show variables 1.表字 ...

  3. hnust 好友互动标识

    问题 A: 好友互动标识 时间限制: 1 Sec  内存限制: 128 MB提交: 897  解决: 122[提交][状态][讨论版] 题目描述       QQ有一个有趣的功能即显示好友互动标识,它 ...

  4. 团队Alpha版本(七)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  5. 使用awk根据多维度统计系统tps

    业务简单日志格式: [2017-08-28 01:00:00.523]transfer_search,7001045,1708271100083774377,20170827,,,,,,,,,,001 ...

  6. MyBatis:SQL语句中的foreach标签的详细介绍

    foreach 也就是遍历迭代,在SQL中通常用在 in 这个关键词的后面 foreach元素的属性主要有 item,index,collection,open,separator,close. 分别 ...

  7. Spring框架jar包分类(转)

    转自:http://www.cnblogs.com/JSONBEAN/p/6364038.html 长期以来都在写SSM框架的项目,却未能深入理解框架的搭建原理,而只是浅薄的理解前辈的架构,然后不断套 ...

  8. Dev express 笔记

    1.设置treelist不同行的颜色 void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDr ...

  9. 堆栈(Stacks)

    堆栈(Stacks) 准备工作 安装Docker 1.13及以上版本 安装Docker Compose正如第三部分的准备工作. 安装Docker Machine正如第四部分的准备工作. 阅读第一部分的 ...

  10. 每天一个小算法(Shell Sort1)

    希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...