Query on a tree II

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

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.

For each "DIST" or "KTH" operation, write one integer representing its result.
Print one blank line after each test.

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

```c++
#include <bits/stdc++.h>
using namespace std;
#define maxn (int)(1e5+10)
#define LL long long

inline int read(){
	int rtn=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))rtn=(rtn<<1)+(rtn<<3)+ch-'0',ch=getchar();
	return rtn*f;
}

struct node{
	int a,b,nt,w;
}e[maxn];

LL w[maxn][21];
int fa[maxn][21],dep[maxn],p[maxn],cnt;

inline void add(int x,int y,int z){
	e[++cnt].a=x;e[cnt].b=y;e[cnt].w=z;
	e[cnt].nt=p[x];p[x]=cnt;
}

inline void dfs(int k){
	for(int i=1;i<=20;i++)fa[k][i]=fa[fa[k][i-1]][i-1];
	for(int i=1;i<=20;i++)w[k][i]=w[fa[k][i-1]][i-1]+w[k][i-1];
	for(int i=p[k];i;i=e[i].nt){
		int kk=e[i].b;
		if(kk==fa[k][0])continue;
		fa[kk][0]=k;dep[kk]=dep[k]+1;w[kk][0]=e[i].w;
		dfs(kk);
	}
}

inline int lca(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=20;i>=0;i--){
		if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
	} 
	if(x==y)return y;
	for(int i=20;i>=0;i--){
		if(fa[x][i]!=fa[y][i])
			x=fa[x][i],y=fa[y][i];
	}return fa[x][0];
}

inline LL dis(int x,int y){
	LL rtn=0;
	int l=lca(x,y);
	for(int i=20;i>=0;i--){
		if(dep[fa[x][i]]>=dep[l])
			rtn+=w[x][i],x=fa[x][i];
	}
	for(int i=20;i>=0;i--){
		if(dep[fa[y][i]]>=dep[l])
			rtn+=w[y][i],y=fa[y][i];
	}return rtn;
}

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

int main(){
	int T=read();
	while(T--){
		int n=read();cnt=0;
		memset(p,0,sizeof(p));
		memset(w,0,sizeof(w));
		memset(fa,0,sizeof(fa));
		for(int i=1;i<n;i++){
			int a=read(),b=read(),w=read();
			add(a,b,w);add(b,a,w);
		}
		dfs(1);
		while(true){
			char ch[10];scanf("%s",ch);
			if(ch[1]=='O')break;
			else if(ch[1]=='T'){
				int x=read(),y=read(),k=read();
				printf("%d\n",kth(x,y,k));
			}
			else if(ch[1]=='I'){
				int x=read(),y=read();
				printf("%lld\n",dis(x,y));
			}
		}
	}
	return 0;
} 
posted @ 2017-12-02 17:10  Tyw_ei  阅读(186)  评论(0编辑  收藏  举报