您的位置:首页 > 产品设计 > UI/UE

Spoj Query on a tree II (LCA)

2014-06-24 10:22 387 查看
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 borKTH a b k : ask for the k-th node on the path from node a to node bExample: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, bof 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
DIST A B 就是dis[a]+dis[b]-2*dis[lca(a,b)];
KTH A B C 先看A和LCA(A,B)的距离,确定答案在哪边。。
/*====================== *File Name: a.cpp *Author: qqspeed *Time: 2014年 06月 24日 星期二 09:10:49 CST  ======================*/#include <stdio.h>#include <string.h>#include <iostream>#include <string>#include <queue>#include <stack>#include <vector>#include <math.h>#include <algorithm>using namespace std;#define rep(i,s,t) for(int i=s;i<t;i++)#define red(i,s,t) for(int i=s-1;i>=t;i--)#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)#define L t<<1#define R t<<1|1#define max(a,b) (a<b?b:a)#define min(a,b) (a<b?a:b)#define clr(a,v) memset(a,v,sizeof a)typedef long long ll;inline int input(){  	int ret=0;bool isN=0;char c=getchar(); 	while(c<'0' || c>'9'){  		if(c=='-') isN=1;  		c=getchar();  	}  	while(c>='0' && c<='9'){		ret=ret*10+c-'0';c=getchar();  	}  	return isN?-ret:ret;  }  inline void output(int x){  	if(x<0){  		putchar('-');x=-x;  	}  	int len=0,data[11];  	while(x){  		data[len++]=x%10;x/=10;  	}  	if(!len)    data[len++]=0;  	while(len--) 		putchar(data[len]+48);  	putchar('\n');}const int MAXN=10005;struct Edge{	int v,w,next;}edge[MAXN<<1];int head[MAXN],e;int t,n,a,b,c;char op[5];int dep[MAXN],dis[MAXN],fa[MAXN][20];inline void addEdge(int u,int v,int w){	edge[e].v=v;	edge[e].w=w;	edge[e].next=head[u];	head[u]=e++;}inline void dfs(int now,int pre,int d,int dd){	fa[now][0]=pre;dep[now]=dd;dis[now]=d;	rep(i,1,20) fa[now][i]=fa[fa[now][i-1]][i-1];	ree(i,now){		int nxt=edge[i].v,w=edge[i].w;		if(nxt!=pre) dfs(nxt,now,d+w,dd+1);	}}inline int query(int xx,int yy){	int x=xx,y=yy;	if(dep[x]>dep[y]) swap(x,y);	int ha=dep[x],hb=dep[y];	int det=hb-ha;	rep(i,0,20){		if(det&(1<<i)){			y=fa[y][i];		}	} 	if(x==y) return x;	red(i,20,0){		if(fa[x][i]==fa[y][i]) continue;		x=fa[x][i];		y=fa[y][i];	}	return fa[x][0];}inline int Find(int x,int det){	rep(i,0,20){		if(det&(1<<i)){			x=fa[x][i];		}	}		return x;}inline int solve(int a,int b,int c){	int lca=query(a,b);	int det=dep[a]-dep[lca]+1;	if(det==c) return lca;	else if(det>c){		return Find(a,c-1);	}	else{		int dd=dep[b]-dep[lca]+1;		det=c-(det-1);		return Find(b,dd-det);	}}int main(){	t=input();	rep(ca,0,t){		n=input();		clr(head,-1),e=0;		rep(i,1,n){			a=input(),b=input(),c=input();			addEdge(a,b,c);			addEdge(b,a,c);		}		dfs(1,1,0,0);		while(scanf("%s",op),op[1]!='O'){			if(op[0]=='D'){				a=input(),b=input();				output(dis[a]+dis[b]-2*dis[query(a,b)]);			}			else{				a=input(),b=input(),c=input();				output(solve(a,b,c));			}		}		puts("");	}	return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: