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
3You 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
树上倍增即可
同时倍增父亲和距离
注意计算中的+1 -1;
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<map>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 10010;
struct node{
int to,cost;
};
int dep[maxn],fa[maxn][30];
int in[maxn],da[maxn][30];
vector<node> adj[maxn];
inline void Init()
{
for(int i=0;i<maxn;i++){
adj[i].clear();
dep[i] = in[i] = 0;
}
memset(fa,0,sizeof(fa));
memset(da,0,sizeof(da));
}
inline void add(int u,int v,int d)
{
node tmp;
tmp.to = v;
tmp.cost = d;
adj[u].push_back(tmp);
}
inline void dfs(int x)
{
for(int i=1;i<=20;i++){
if(fa[x][i-1]){
fa[x][i] = fa[fa[x][i-1]][i-1];
}
else break;
}
for(int i=1;i<=20;i++)
da[x][i] = da[x][i-1] + da[fa[x][i-1]][i-1];
for(int i=0;i<adj[x].size();i++){
int v = adj[x][i].to;
if(v!=fa[x][0]){
fa[v][0] = x;
da[v][0] = adj[x][i].cost;
dep[v] = dep[x] + 1;
dfs(v);
}
}
}
inline int LCA(int u,int v)
{
if(dep[u]<dep[v]) swap(u,v);
int delta = dep[u] - dep[v];
for(int x=0;x<=20;x++){
if((1<<x)&delta)
u=fa[u][x];
}
if(u==v) return u;
for(int x=20;x>=0;x--){
if(fa[u][x]!=fa[v][x]){
u=fa[u][x];
v=fa[v][x];
}
}
return fa[u][0];
}
inline int kth(int x,int y,int k){
int l = LCA(x,y);
if(k<=dep[x]-dep[l]){
k -= 1;
for(int i=20;i>=0;i--){
if(1<<i<=k){
x = fa[x][i];
k -= 1<<i;
}
}
return x;
}
else if(k>dep[x]-dep[l]){
k = (dep[y]-dep[l]-(k-(dep[x]-dep[l])))+1;
for(int i=20;i>=0;i--){
if(1<<i<=k){
y = fa[y][i];
k -= 1<<i;
}
}
return y;
}
}
inline int dis(int x,int y){
int rtn = 0;
int l = LCA(x,y);
for(int i=20;i>=0;i--){
if(dep[fa[x][i]]>=dep[l]){
rtn += da[x][i];
x = fa[x][i];
}
}
for(int i=20;i>=0;i--){
if(dep[fa[y][i]]>=dep[l]){
rtn += da[y][i];
y = fa[y][i];
}
}
return rtn;
}
int main(void)
{
int t,n;
int u,v,d;
char cmd[10];
scanf("%d",&t);
while(t--){
Init();
scanf("%d",&n);
for(int i=1;i<n;i++){
scanf("%d%d%d",&u,&v,&d);
add(u,v,d);add(v,u,d);
}
dfs(1);
while(1){
scanf("%s",cmd);
if(strcmp(cmd,"DONE")==0) break;
else if(strcmp(cmd,"DIST")==0){
scanf("%d%d",&u,&v);
printf("%d\n",dis(u,v));
}
else{
scanf("%d%d%d",&u,&v,&d);
printf("%d\n",kth(u,v,d));
}
}
}
return 0;
}