SPOJ 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
    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). ttest 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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lca~

类似于lca的求法,预处理出fa[i][j],val[i][j]和dep[i],分别表示i的2^j祖先,i到2^j祖先的距离和i的层数,对于DIST询问,求lca的同时计算出来val的总和直接输出;对于KTH询问,求lca的同时计算出两点之间的线段数num,然后直接求x的z祖先,如果高于公共祖先k,就求y的num-z祖先,直接输出。要注意的是这里的z在输入后要减一表示线段数,方便计算。

惨痛的教训:一定要认真看题目里面要求的格式!!!


#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

int t,n,x,y,z,fi[10001],w[20001],ne[20001],v[20001],dep[10001],fa[10001][17],val[10001][17],cnt,num;
char s[10];

void add(int u,int vv,int val)
{
	w[++cnt]=vv;ne[cnt]=fi[u];fi[u]=cnt;v[cnt]=val;
	w[++cnt]=u;ne[cnt]=fi[vv];fi[vv]=cnt;v[cnt]=val;
}

int cal(int u,int v)
{
	int tot=0;
	if(dep[u]<dep[v]) swap(u,v);
	for(int i=16;i>=0;i--)
	  if(dep[fa[u][i]]>=dep[v]) tot+=val[u][i],u=fa[u][i];
	if(u==v) return tot;
	for(int i=16;i>=0;i--)
	  if(fa[u][i]!=fa[v][i]) tot+=val[u][i]+val[v][i],u=fa[u][i],v=fa[v][i];
	return tot+val[u][0]+val[v][0];
}

int lca(int u,int v)
{
	num=0;
	if(dep[u]<dep[v]) swap(u,v);
	for(int i=16;i>=0;i--)
	  if(dep[fa[u][i]]>=dep[v]) u=fa[u][i],num+=(1<<i);
	if(u==v) return u;
	for(int i=16;i>=0;i--)
	  if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i],num+=2*(1<<i);num+=2;
	return fa[u][0]; 
}

void dfs(int u)
{
	for(int i=fi[u];i;i=ne[i])
	  if(fa[u][0]!=w[i])
	  {
	  	fa[w[i]][0]=u;val[w[i]][0]=v[i];
		dep[w[i]]=dep[u]+1;dfs(w[i]);
	  }
}

void init()
{
	for(int j=1;j<=16;j++)
	  for(int i=1;i<=n;i++)
	    fa[i][j]=fa[fa[i][j-1]][j-1],val[i][j]=val[i][j-1]+val[fa[i][j-1]][j-1];
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		memset(fi,0,sizeof(fi));cnt=0;dep[1]=1;
		memset(val,0,sizeof(val));
		memset(fa,0,sizeof(fa));
		scanf("%d",&n);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d%d",&x,&y,&z);add(x,y,z);
		}
		dfs(1);init();
		while(1)
		{
			scanf("%s",s);
			if(s[1]=='O') break;
			if(s[0]=='D')
			{
				scanf("%d%d",&x,&y);printf("%d\n",cal(x,y));
			}
			else
			{
				scanf("%d%d%d",&x,&y,&z);z--;
				int k=lca(x,y),xx=x,yy=y;
				for(int i=16;i>=0;i--)
				  if(z&(1<<i)) xx=fa[xx][i];
				if(dep[xx]<dep[k] || !xx)
				{
					z=num-z;
					for(int i=16;i>=0;i--)
					  if(z&(1<<i)) yy=fa[yy][i];
					printf("%d\n",yy);
				}
				else printf("%d\n",xx);
			}
		}
		if(t) printf("\n");
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值