SPOJ 375.Query on a tree

本文介绍了一种解决树形结构上查询与更新问题的高效算法。通过将边权转化为点权,利用link-cut tree(链割树)实现动态修改与路径最大值查询。文章详细解释了算法的基本思想及其实现细节,包括数据结构定义、旋转操作、Splay树应用等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SPOJ Problem Set (classical)
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 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
最基本的思想是边权转点权
采用link-cut tree进行动态修改 CHANGE操作把目标节点移动到Splay森林(即link-cut tree)的根节点,并对其进行修改维护 QUERY操作则先对其中一点A进行Access,然后对另一点B进行Access途中进行输出,也就是在最后一次的树链合并前对T[last].maxx和T[T[root].s[1]].maxx取较大值输出即可,由于第一次的Access操作T[root].s[1]的maxx也就是点A到公共祖先的边的最大值,因此可以验证这一算法的正确性 指针版本就不贴了,思路完全一样不过速度略胜一筹,以下是数组版本
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#define MAXN 100010
using namespace std;
vector<pair<int,int> > edge;
struct list{
	int p,val;
	list *next;
	list(int _p,int _val,list* _next){
		p=_p; val=_val; next=_next;
	}
}*head[MAXN];
struct tree{
	int s[2],f,cost,maxx;
	bool ws,root;
	tree(){f=-1; cost=maxx=0;}
}T[MAXN];int f[MAXN];
void Sets(int f,int p,bool ws){T[f].s[ws]=p; T[p].f=f; T[p].ws=ws;}
void Maintain(int p){T[p].maxx=max(max(T[T[p].s[0]].maxx,T[T[p].s[1]].maxx),T[p].cost);}
void BFS(){
	queue <int> q;
	q.push(1); T[1].f=0;
	while(q.size()){
		int u=q.front(); q.pop();
		for(list *k=head[u];k;k=k->next)
			if(T[k->p].f==-1){
				T[k->p].cost=T[k->p].maxx=k->val;
				f[k->p]=T[k->p].f=u; q.push(k->p);
			}
	}
}
void Rotate(int p){
	bool ws=T[p].ws;int f=T[p].f;
	if(T[f].f!=0) Sets(T[f].f,p,T[f].ws);else T[p].f=0;
	if(T[p].s[ws^1]) Sets(f,T[p].s[ws^1],ws);else T[f].s[ws]=0;
	Sets(p,f,ws^1); if(T[f].root) T[f].root=0,T[p].root=1;
	Maintain(f); Maintain(p);
}
void Splay(int p){
	while(!T[p].root){
		int f=T[p].f;
		if(T[T[p].f].root) Rotate(p);
		else if(T[p].ws==T[f].ws) Rotate(f),Rotate(p);
		else Rotate(p),Rotate(p);
	}
}
void Access(int p,int use){
	int last=0;
	while(p!=0){
		Splay(p);
		if(!T[p].f&&use==2) printf("%d\n",max(T[p].s[1],T[last].maxx));
		T[T[p].s[1]].root=1;
		if(last!=0) Sets(p,last,1);else T[p].s[1]=0; 
		T[last].root=0;
		Maintain(p);
		last=p; p=T[p].f;
	}
}
void Query(int u,int v){
	Access(u,1);
	Access(v,2);
}
void Change(int p,int val){
	Access(p,1);
	Splay(p);
	T[p].cost=val;
	Maintain(p);
}
int main(){
	//freopen("QTREE1.in","r",stdin);
	//freopen("QTREE1.out","w",stdout);
	int t,N,a,b,c; char str[20];
	scanf("%d",&t);
	while(t--){
		scanf("%d",&N);
		for(int i=1;i<=N;i++) T[i].root=1;
		for(int i=1;i<=N-1;i++){
			scanf("%d%d%d",&a,&b,&c);
			head[a]=new list(b,c,head[a]);
			head[b]=new list(a,c,head[b]);
			edge.push_back(make_pair(a,b));
		}BFS();
		//for(int i=1;i<=N;i++) printf("T%d f:%d maxx:%d ls:%d rs:%d cost:%d\n",i,T[i].f,T[i].maxx,T[i].s[0],T[i].s[1],T[i].cost);
		while(1){
			scanf("%s",str);
			if(strcmp(str,"QUERY")==0){
				scanf("%d%d",&a,&b);
				Query(a,b);
			}else if(strcmp(str,"CHANGE")==0){
				scanf("%d%d",&a,&b); a--;
				int u=edge[a].first,v=edge[a].second;
				if(f[u]==v) Change(u,b); else Change(v,b);
			}else if(strcmp(str,"DONE")==0) break;
		}
	}
	return 0;
} 









                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值