SPOJ 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 a, b 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

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 10005
int g[maxn];
struct node{
	int to,nxt,vl;
}ed[maxn<<1];
int head[maxn],cnt;
void addedge(int u,int v,int vl){
	ed[cnt].to=v;
	ed[cnt].vl=vl;
	ed[cnt].nxt=head[u];
	head[u]=cnt++;
}

int sz[maxn],top[maxn],son[maxn],fa[maxn],p[maxn],dep[maxn];
int pos;
void dfs1(int u,int f,int d){
	//printf("%d %d %d\n",u,f,d);
	dep[u]=d;	
	sz[u]=1;
	fa[u]=f;
	for(int i=head[u];~i;i=ed[i].nxt){
		int v=ed[i].to;
		if(v==f)continue;
		dfs1(v,u,d+1);
		sz[u]+=sz[v];
		if(son[u]==-1||sz[son[u]]<sz[v])son[u]=v;
	}
}
void dfs2(int u,int tp){
	//printf("%d %d\n",u,tp);
	top[u]=tp;
	p[u]=pos++;
	if(son[u]==-1)return;
	dfs2(son[u],tp);
	for(int i=head[u];~i;i=ed[i].nxt){
		int v=ed[i].to;
		if(v!=son[u]&&v!=fa[u]){
			dfs2(v,v);
		}
	}
}

struct T{
	int l,r,ma;
}stu[maxn*4];
void pushup(int rt){
	if(stu[rt].l==stu[rt].r)return ;
	stu[rt].ma=max(stu[rt<<1].ma,stu[(rt<<1)+1].ma);
	return ;
}
void build(int l,int r,int rt){
	stu[rt].l=l,stu[rt].r=r;
	if(l==r){
		stu[rt].ma=g[l];
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,rt<<1);
	build(mid+1,r,(rt<<1)+1);
	pushup(rt);
	return ;
}
void updata(int a,int b,int r){
	if(stu[r].l==stu[r].r)
	{stu[r].ma=b,g[a]=b;
	return ;}
	int mid=(stu[r].l+stu[r].r)>>1;
	if(a>mid)updata(a,b,(r<<1)+1);
	else updata(a,b,r<<1);
	pushup(r);
	return ;
}
int q(int l,int r,int rt){
	if(stu[rt].l==l&&stu[rt].r==r)return stu[rt].ma;
	int mid=(stu[rt].l+stu[rt].r)>>1;
	if(mid>=r)return q(l,r,rt<<1);
	else if(mid<l)return q(l,r,(rt<<1)+1);
	else return max(q(l,mid,rt<<1),q(mid+1,r,(rt<<1)+1));
}
int getma(int u,int v){
	int f1=top[u],f2=top[v];
	int ans=0;
	while(f1!=f2){
		if(dep[f1]<dep[f2]){
			swap(f1,f2);
			swap(u,v);
		}
		ans=max(ans,q(p[f1],p[u],1));
		u=fa[f1];
		f1=top[u];
	}
	if(u==v)return ans;
	if(dep[u]<dep[v])swap(u,v);
	ans=max(ans,q(p[son[v]],p[u],1));
	return ans;
}
int uu[maxn];
void init(){
	memset(head,-1,sizeof(head));
	cnt=0;
	memset(son,-1,sizeof(son));
	pos=0;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		init();
		int n;
		scanf("%d",&n);
		for(int i=1;i<n;i++){
			int u,v,vl;
			scanf("%d%d%d",&u,&v,&vl);
			addedge(u,v,vl);
			addedge(v,u,vl);
		}
		dfs1(1,0,0);
		dfs2(1,1);
		for(int i=0;i<cnt;i+=2){
			int v=ed[i].to,vl=ed[i].vl;
			int u=ed[i^1].to;
			if(dep[u]<dep[v])swap(u,v);
			g[p[u]]=vl;
			uu[i/2+1]=u;
		}
		build(0,n-1,1);
		char str[10];
		while(~scanf("%s",str)){
			if(str[0]=='D')break;
			int a,b;
			scanf("%d%d",&a,&b);
			if(str[0]=='C'){
				updata(p[uu[a]],b,1);
			}
			else printf("%d\n",getma(a,b));
		}
	}
	return 0;
}



                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值