QTREE2 – Query on a tree II

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

Sample 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

Sample Output

5
3

题目分析:

LCA 的小变形 第一问问你两点路径之和 第二问问你两点之间的路径上的第k的点(包含起始点,结束点)
第一问我们类似货车运输就搞定了 第二问的话,用类似倍增的方法,一点一点往上移,直到第k个点为止;
这里还要讨论deep[lca(x,y)] deep[x] k 之间的大小关系 确认第k个点是在哪条分支上。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int net[21000],head[21000],to[21000],val[21000];
int deep[21000],fa[21000][22];
long long sum[21000][22];
int t;
int n;
int tot;
void init()
{
	tot=0;
	memset(net,0,sizeof net);
	memset(head,0,sizeof head);
	memset(to,0,sizeof to);
	memset(val,0,sizeof val);
	memset(deep,0,sizeof deep);
	memset(fa,0,sizeof fa);
	memset(sum,0,sizeof sum);
}
void add(int x,int y,int c)
{
	net[++tot]=head[x];
	val[tot]=c;
	to[tot]=y;
	head[x]=tot;
}
char s[10];
void dfs(int x,int temp)
{
	deep[x]=deep[temp]+1;
	fa[x][0]=temp;
	for(int i=1;fa[x][i-1];i++)
	{
		fa[x][i]=fa[fa[x][i-1]][i-1];
		sum[x][i]=sum[x][i-1]+sum[fa[x][i-1]][i-1];
	}
	for(int i=head[x];i;i=net[i])
		if(to[i]!=temp)
		{
			sum[to[i]][0]=val[i];
			dfs(to[i],x);
		}
}
long long lca(int x,int y,int color)
{
	if(deep[x]<deep[y]) swap(x,y);
	int temp=19;
	long long tmp=0;
	while(temp>=0)
	{
		if(deep[fa[x][temp]]>=deep[y]) tmp+=sum[x][temp],x=fa[x][temp];
		temp--;
	}
	temp=19;
	if(x==y) return color==1?tmp:x;
	while(temp>=0)
	{
		if(fa[x][temp]!=fa[y][temp])
			tmp=tmp+sum[x][temp]+sum[y][temp],x=fa[x][temp],y=fa[y][temp];
		temp--;
	}
	return color==1?tmp+sum[x][0]+sum[y][0]:fa[x][0];
}
int find(int x,int y,int k)
{
	int LCA=lca(x,y,0);
	k--;
    if(deep[x]-deep[LCA]<k)
    {
        k=deep[x]-deep[LCA]*2+deep[y]-k;
        x=y;
    }
    for(int i=0;i<20;i++)
        if(k&(1<<i)) x=fa[x][i];
    return x;
}
int main()
{
	scanf("%d",&t);
	while(t)
	{
		t--;
		init();
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{
			int x,y,c;
			scanf("%d%d%d",&x,&y,&c);
			add(x,y,c),add(y,x,c);			
		}
		dfs(1,0);
		while(1)
		{
			scanf("%s",&s[0]);	
			if(s[0]=='D'&&s[1]=='O') break;
			if(s[0]=='D')
			{
				int x,y;
				scanf("%d%d",&x,&y);
				printf("%lld\n",lca(x,y,1));
			}			
			else
			{
				int x,y,k;
				scanf("%d%d%d",&x,&y,&k);
				printf("%d\n",find(x,y,k));
			}
		}
	}
    return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注