SPOJ 375 Query on a tree

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1.

We will ask you to perfrom some instructions of the following form:

  • CHANGE i ti : change the cost of the i-th edge to ti
    or
  • QUERY a b : ask for the maximum edge cost on the path from node a to node b

Input

The first line of input contains an integer t, the number of test cases (t <= 20). 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 <= 1000000),
  • The next lines contain instructions "CHANGE i ti" or "QUERY a b",
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "QUERY" operation, write one integer representing its result.

Example

Input:
1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Output:
1
3

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

树链剖分~

个人理解:树链剖分是一种把树上的链划分为区间,再用线段树维护的算法。可以证明轻链重链至多分别有logn条,所以复杂度较小。

重儿子:点u的子节点中子树最大的点。

重链:由重儿子组成的长链。

我们对树中的点重新标号使得每一条重链上的点编号都是连续的,在查询时就可以方便地求出某一条链上的答案,用来更新最终答案。


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

int t,n,x,y,z,fi[10001],ne[20001],w[20001],v[20001],top[10001],son[10001],dep[10001],cnt,fa[10001],c[10001<<2],cost[10001],siz[10001],id[10001];
char ch[6];

struct node{
	int x,y,val;
}a[10001];

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<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

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

void dfs1(int u)
{
	son[u]=0;siz[u]=1;
	for(int i=fi[u];i;i=ne[i])
	  if(w[i]!=fa[u])
	  {
	  	dep[w[i]]=dep[u]+1;fa[w[i]]=u;
	  	dfs1(w[i]);siz[u]+=siz[w[i]];
	  	if(!son[u] || siz[son[u]]<siz[w[i]]) son[u]=w[i];
	  }
}

void dfs2(int u,int to)
{
	id[u]=++cnt;top[u]=to;
	if(son[u]) dfs2(son[u],to);
	for(int i=fi[u];i;i=ne[i])
	  if(w[i]!=fa[u] && w[i]!=son[u]) dfs2(w[i],w[i]);
}

void build(int l,int r,int k)
{
	if(l==r)
	{
		c[k]=cost[l];return;
	}
	int mid=l+r>>1;
	build(l,mid,k<<1);build(mid+1,r,k<<1|1);
	c[k]=max(c[k<<1],c[k<<1|1]);
}

void chan(int l,int r,int k,int u,int v)
{
	if(l==r)
	{
		c[k]=v;return;
	}
	int mid=l+r>>1;
	if(u<=mid) chan(l,mid,k<<1,u,v);
	else chan(mid+1,r,k<<1|1,u,v);
	c[k]=max(c[k<<1],c[k<<1|1]);
}

int cal(int l,int r,int k,int u,int v)
{
	if(l>=u && r<=v) return c[k];
	int now=0,mid=l+r>>1;
	if(mid>=u) now=max(now,cal(l,mid,k<<1,u,v));
	if(mid<v) now=max(now,cal(mid+1,r,k<<1|1,u,v));
	return now;
}

int query(int u,int v)
{
	int x=top[u],y=top[v],now=0;
	while(x!=y)
	{
		if(dep[x]<dep[y]) swap(u,v),swap(x,y);
		now=max(now,cal(1,n,1,id[x],id[u]));
		u=fa[x];x=top[u];
	}
	if(dep[u]>dep[v]) swap(u,v);
	else if(dep[u]==dep[v]) return now;
	return max(now,cal(1,n,1,id[u]+1,id[v]));
}

int main()
{
	t=read();
	while(t--)
	{
		n=read();cnt=0;
		memset(fi,0,sizeof(fi));
		memset(a,0,sizeof(a));
		for(int i=1;i<n;i++)
		{
			x=read();y=read();z=read();a[i]=(node){x,y,z};add(x,y,z);
		}
		cnt=0;dfs1(1);dfs2(1,1);
		for(int i=1;i<n;i++)
		{
			if(dep[a[i].x]>dep[a[i].y]) swap(a[i].x,a[i].y);cost[id[a[i].y]]=a[i].val;
		}
		build(1,n,1);
		while(scanf("%s",ch)==1)
		{
			if(ch[0]=='D') break;
			x=read();y=read();
			if(ch[0]=='C') chan(1,n,1,id[a[x].y],y);
			else printf("%d\n",query(x,y));
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值