spoj 375 QTREE - Query on a tree 树链剖分边权模板

题目链接:spoj 375

QTREE - 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


题意:给出一棵树,每个边有一个边权,有2种操作,一种是改变第i个边的边权,另一种是查询从a到b的路径边权最大值。

题目分析:树链剖分的模板,2次dfs,之后映射到线段树上求区间最大值。

//
//  main.cpp
//  spoj 375
//
//  Created by teddywang on 2017/9/28.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=11111;
int T,n,m;
int dep[maxn],size[maxn],fa[maxn],id[maxn],son[maxn],val[maxn],top[maxn];
int num;
vector<int> v[maxn];
struct tree{
    int x,y,val;
    void read(){
        scanf("%d%d%d",&x,&y,&val);
    }
}e[maxn];
struct Tree{
    int l,r,val;
}tree[4*maxn];

void dfs1(int u,int f,int d)
{
    dep[u]=d;
    size[u]=1;
    son[u]=0;
    fa[u]=f;
    int len=v[u].size();
    for(int i=0;i<len;i++)
    {
        int ff=v[u][i];
        if(ff==f) continue;
        else
        {
            dfs1(ff,u,d+1);
            size[u]+=size[ff];
            if(size[son[u]]<size[ff])
            son[u]=ff;
        }
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=++num;
    if(son[u]) dfs2(son[u],tp);
    int len=v[u].size();
    for(int i=0;i<len;i++)
    {
        int ff=v[u][i];
        if(ff==fa[u]||ff==son[u]) continue;
        dfs2(ff,ff);
    }
}

void pushup(int x)
{
    tree[x].val=max(tree[x<<1].val,tree[x<<1|1].val);
}

void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    if(l==r)
    {
        tree[rt].val=val[l];
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int u,int v,int val)
{
    if(tree[u].l==tree[u].r)
    {
        tree[u].val=val;
        return;
    }
    int mid=(tree[u].l+tree[u].r)>>1;
    if(v<=mid)
        update(u<<1,v,val);
    else update(u<<1|1,v,val);
    pushup(u);
}

int query(int x,int l,int r)
{
    if(tree[x].l>=l&&tree[x].r<=r)
    {
        return tree[x].val;
    }
    int mid=(tree[x].l+tree[x].r)>>1;
    int ans=0;
    if(l<=mid) ans=max(ans,query(x<<1,l,r));
    if(r>mid) ans=max(ans,query(x<<1|1,l,r));
    return ans;
}

int Yougth(int u, int v) {
    int tp1 = top[u], tp2 = top[v];
    int ans = 0;
    while (tp1 != tp2) {
        //printf("YES\n");
        if (dep[tp1] < dep[tp2]) {
            swap(tp1, tp2);
            swap(u, v);
        }
        ans = max(query(1,id[tp1], id[u]), ans);
        u = fa[tp1];
        tp1 = top[u];
    }
    if (u == v) return ans;
    if (dep[u] > dep[v]) swap(u, v);
    ans = max(query(1,id[son[u]], id[v]), ans);
    return ans;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            e[i].read();
            v[e[i].x].push_back(e[i].y);
            v[e[i].y].push_back(e[i].x);
        }
        num=0;
        dfs1(1,0,1);
        dfs2(1,1);
        for(int i=1;i<n;i++)
        {
            if(dep[e[i].x]<dep[e[i].y]) swap(e[i].x,e[i].y);
            val[id[e[i].x]]=e[i].val;
        }
        build(1,num,1);
        char s[20];
        while(scanf("%s",s))
        {
            if(s[0]=='D') break;
            int x,y;
            scanf("%d%d",&x,&y);
            if(s[0]=='Q')
                printf("%d\n",Yougth(x,y));
            else
            {
                update(1,id[e[x].x],y);
            }
        }
        for(int i=0;i<=n;i++)
            v[i].clear();
    }
}


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、付费专栏及课程。

余额充值