SPOJ 题目375 Query on a tree(link cut tree边权更新,求两点最大值)

QTREE - Query on a tree

no tags 

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<stdio.h>   
#include<string.h>   
#include<stdlib.h>   
#include<queue>   
#include<iostream>   
#define max(a,b) (a>b?a:b)
using namespace std;  
int head[50050],cnt,vis[50050];  
struct s  
{  
    int u,v,w,next;  
}edge[50050<<1];  
struct LCT    
{    
    int bef[50050],pre[50050],next[50050][2],key[50050],val[50050],belong[50050];  
    void init()    
    {    
        memset(pre,0,sizeof(pre));  
        memset(next,0,sizeof(next));    
    }  
    void pushup(int x)  
    {  
        val[x]=max(key[x],max(val[next[x][1]],val[next[x][0]]));  
    }  
    void rotate(int x,int kind)    
    {    
        int y,z;    
        y=pre[x];    
        z=pre[y];    
        next[y][!kind]=next[x][kind];    
        pre[next[x][kind]]=y;    
        next[z][next[z][1]==y]=x;    
        pre[x]=z;    
        next[x][kind]=y;    
        pre[y]=x;  
        pushup(y);  
    }    
    void splay(int x)    
    {    
        int rt;    
        for(rt=x;pre[rt];rt=pre[rt]);    
        if(x!=rt)    
        {    
            bef[x]=bef[rt];    
            bef[rt]=0;    
            while(pre[x])    
            {    
                if(next[pre[x]][0]==x)    
                {    
                    rotate(x,1);    
                }    
                else    
                    rotate(x,0);    
            }    
            pushup(x);  
        }    
    }   
    void access(int x)    
    {    
        int fa;    
        for(fa=0;x;x=bef[x])    
        {    
            splay(x);    
            pre[next[x][1]]=0;    
            bef[next[x][1]]=x;    
            next[x][1]=fa;    
            pre[fa]=x;    
            bef[fa]=0;    
            fa=x;    
            pushup(x);  
        }    
    }  
    void change(int x,int val)  
    {  
        int t;  
        t=belong[x-1];  
        key[t]=val;  
        splay(t);  
    }  
    int query(int x,int y)  
    {  
        access(y);  
        for(y=0;x;x=bef[x])  
        {  
            splay(x);  
            if(!bef[x])  
                return max(val[y],val[next[x][1]]);  
            pre[next[x][1]]=0;  
            bef[next[x][1]]=x;  
            next[x][1]=y;  
            pre[y]=x;  
            bef[y]=0;  
            y=x;  
            pushup(x);  
        }  
    }  
}lct;  
void add(int u,int v,int w)  
{  
    edge[cnt].u=u;  
    edge[cnt].v=v;  
    edge[cnt].w=w;  
    edge[cnt].next=head[u];  
    head[u]=cnt++;  
}  
void bfs(int u)  
{  
    int i,y;  
    queue<int>q;  
    memset(vis,0,sizeof(vis));  
    vis[u]=1;  
    q.push(u);  
    while(!q.empty())  
    {  
        u=q.front();  
        q.pop();  
        for(int i=head[u];i!=-1;i=edge[i].next)  
        {  
            int v=edge[i].v;  
            if(!vis[v])  
            {  
                lct.bef[v]=u;  
                lct.key[v]=lct.val[v]=edge[i].w;  
                lct.belong[i>>1]=v;  
                vis[v]=1;  
                q.push(v);  
            }  
        }  
    }  
}  
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		memset(head,-1,sizeof(head));
		cnt=0;
		int i;
		char s[10];
		scanf("%d",&n);
		for(i=1;i<n;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);
			add(b,a,c);
		}
		lct.init();
		bfs(1);
		while(scanf("%s",s)!=EOF)
		{
			int a,b;
			if(strcmp(s,"DONE")==0)
				break;
			scanf("%d%d",&a,&b);
			if(s[0]=='Q')
			{
				printf("%d\n",lct.query(a,b));
			}
			else
				lct.change(a,b);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值