SPOJ375--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
树链剖分入门题了。不懂可以先看这篇博文:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 100080
#define maxm 200160
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
int first[maxn],e;
int vv[maxm],nxt[maxm],ww[maxm];
int fa[maxn];//父亲节点
int top[maxn];//top[v]表示v所在的重链的顶端节点
int dep[maxn];//深度
int size[maxn];//size[v]表示以v为根的子树的节点数
int p[maxn];//p[v]表示以v与其父亲节点的连边在线段树中的位置
int fp[maxn];//和数组相反
int son[maxn];//重儿子
int pos;

//dfs1函数确定fa,dep,size,son
void init()
{
	pos = 1;
	e = 0;
	memset(first,-1,sizeof(first));
	memset(son,-1,sizeof(son));
}

void addedge(int u,int v,int w)
{
	vv[e] = v;	ww[e] = w;	nxt[e] = first[u];	first[u] = e++;
	vv[e] = u;	ww[e] = w;	nxt[e] = first[v];	first[v] = e++;
}

void dfs1(int u,int pre,int d)
{
	fa[u] = pre;
	dep[u] = d;
	size[u] = 1;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(v == pre)	continue;
		dfs1(v,u,d+1);
		size[u] += size[v];
		if(son[u] == -1 || size[v] > size[son[u]])
			son[u] = v;
	}
}

//dfs2确定top,p
void dfs2(int u,int t)
{
	top[u] = t;
	if(son[u] != -1)
	{
		p[u] = pos++;//p[u]表示连向u的这条边在线段树中的位置
		fp[p[u]] = u;
		dfs2(son[u],t);
	}
	else 
	{
		p[u] = pos++;
		fp[p[u]] = u;
		return;
	}
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(v == son[u] || v == fa[u])	continue;
		dfs2(v,v);
	}
}


struct ST
{
	int l,r,Max;
}st[maxn<<2];

void build(int id,int l,int r)
{
	st[id].l = l;st[id].r = r;
	st[id].Max = 0;
	if(l == r)	return;
	int mid = (l+r) >> 1;
	build(lson);
	build(rson);
}

void PushUp(int id)
{
	st[id].Max = max(st[id<<1].Max,st[id<<1|1].Max);
}

void Update(int id,int pos,int k)
{
	if(st[id].l == pos && st[id].r == pos)
	{
		st[id].Max = k;
		return;
	}
	if(st[id<<1].r >= pos)
		Update(id<<1,pos,k);
	else if(st[id<<1|1].l <= pos)
		Update(id<<1|1,pos,k);
	PushUp(id);
}

int query(int id,int l,int r)
{
	if(st[id].l == l && st[id].r == r)	return st[id].Max;
	int mid = (l+r) >> 1;
	if(st[id<<1].r >= r)
		return query(id<<1,l,r);
	else if(st[id<<1|1].l <= l)
		return query(id<<1|1,l,r);
	return max(query(id<<1,l,st[id<<1].r),query(id<<1|1,st[id<<1|1].l,r));
}

int find(int u,int v)//找u和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,query(1,p[f1],p[u]));
		u = fa[f1];	f1 = top[u];
	}
	if(u == v)	return ans;
	if(dep[u] > dep[v])	swap(u,v);
	return max(ans,query(1,p[son[u]],p[v]));
}

int main()
{
	int T;
	int n;
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d",&n);
		for(int i = 0;i < n-1;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			addedge(u,v,w);
		}
		dfs1(1,0,0);
		dfs2(1,1);
		build(1,1,n);
		char ope[10];
		for(int i = 0;i < n;i++)
		{
			for(int j = first[i];j != -1;j = nxt[j])
			{
				int v = vv[j];
				if(dep[i] < dep[v])
				{
					Update(1,p[v],ww[j]);
				}
			}
		}
		while(scanf("%s",ope)!=EOF && ope[0] != 'D')
		{
			int u,v;	
			scanf("%d%d",&u,&v);
			if(ope[0] == 'Q')
			{
				printf("%d\n",find(u,v));
			}
			else 
			{
				int cnt1 = 2*u-1;
				int cnt2 = 2*u-2;
				int cnt3 = dep[vv[cnt1]] > dep[vv[cnt2]]?vv[cnt1]:vv[cnt2];
				Update(1,p[cnt3],v);
			}
		}
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值