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 ;
}

最新文章

  1. C语言-指针
  2. hammer.js实现背景图手势缩放调整位置
  3. android自定义控件(3)-自定义当前按钮属性
  4. java9-1.类,抽象类,接口的综合小练习
  5. .net 时间戳和日期互转 【转】http://www.cnblogs.com/zhuiyi/p/5307540.html
  6. VC 宏与预处理使用方法总结
  7. 关于Servlet的PrintWriter 中文乱码问题
  8. Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色
  9. 第一篇:数据工程师眼中的智能电网(Smart Grid)
  10. win32系统信息获取
  11. MyEclipse10+Jdk1.7+OSGI+MySql实现CRUD数据库
  12. mysql语句的一个问题
  13. Android之通过网络播放一首简单的音乐
  14. vue的组件和生命周期
  15. DB2常用函数
  16. android studio——替换全局的某个字符串
  17. 关于虹软人脸识别SDK的接入
  18. MT【28】内心外衣下的等腰三角形个数
  19. c#基础在winform操作数据库,实现增删改查
  20. POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)

热门文章

  1. Python 实现随机打乱字符串
  2. 生成器 yield, next ,send
  3. python基础实践(四)
  4. [Leetcode/Javascript] 461.Hamming Distance
  5. CodeSimth - .Net Framework Data Provider 可能没有安装。解决方法[转载 ]
  6. 深入理解CSS中的margin
  7. JavaWeb笔记(五)JSP
  8. 用Linkedhashmap的LRU特性及SoftReference软引用构建二级缓存
  9. 团队Alpha版本(九)
  10. Java 多线程(Thread)学习