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

SPOJ913 Query on a tree II

2016-11-24 17:40 447 查看
Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu

Description

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

Hint

Added by: Thanh-Vy Hua
Date: 2006-08-27
Time limit: 0.433s
Source limit: 15000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: All except: ERL JS NODEJS PERL 6 VB.net
Resource: Special thanks to Ivan Krasilnikov for his alternative solution

 

 

有两种操作,一是求两点间距离,二是求一点到另一点路径上的第k个点。

LCA妥妥的。

 

/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=10010;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,dis;
}e[mxn<<1];
int hd[mxn],mct=0;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].dis=d;hd[u]=mct;return;
}
int T,n;
int fa[mxn][15];
int dep[mxn];
int dis[mxn];
void init(){memset(hd,0,sizeof hd);memset(fa,0,sizeof fa);mct=0;}
void DFS(int u,int f){
dep[u]=dep[f]+1;
for(int i=1;i<15;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
fa[v][0]=u;
dis[v]=dis[u]+e[i].dis;
DFS(v,u);
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=14;i>=0;i--)
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return y;
for(int i=14;i>=0;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
inline int dist(int x,int y){//求距离
int tmp=LCA(x,y);
return dis[x]+dis[y]-dis[tmp]*2;
}
inline int find(int x,int k){//上溯
for(int i=14;i>=0;i--){
if(k&(1<<i))x=fa[x][i];
}
return x;
}
inline int solve(int x,int y,int k){//查询从x到y路径上第k个结点
int tmp=LCA(x,y);
int mid=dep[x]-dep[tmp]+1;
if(k==mid)return tmp;
if(k>mid){
int dd=dep[y]-dep[tmp]+1;
mid=k-mid+1;
k=dd-mid;
return find(y,k);
}
else
return find(x,k-1);
}
int main(){
T=read();
int i,j,x,y,d;
while(T--){
init();
n=read();
for(i=1;i<n;i++){
x=read();y=read();d=read();
add_edge(x,y,d);
add_edge(y,x,d);
}
int rt=n/2+1;
dis[rt]=0;
DFS(rt,0);
char op[10];
while(scanf("%s",op) && (op[0]!='D' || op[1]!='O')){
if(op[0]=='K'){
x=read();y=read();d=read();
printf("%d\n",solve(x,y,d));
}
if(op[0]=='D'){
x=read();y=read();
printf("%d\n",dist(x,y));
}
}
}
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: