[SPOJ375] QTREE - Query on a tree

Description

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
  • 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.

题目大意

给定一棵树,告诉了每条边的权值,然后给出两种操作:
(1)把第i条边的权值改为val
(2)询问a,b路径上权值最大的边

题解

树链剖分教程http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
学了树链剖分,这种算法是把树hash到了几段连续的区间上,分重链和轻链为连续的区间,可以证明任何两点间路径都可以分为 logn 复杂度的重链和轻链,也就是连续区间,然后再用线段树等算法在这些区间上处理问题。显然本题也是这样,剖分过程 O(n) 复杂度,两次dfs,用在 logn 的复杂度套上线段树 O(nlogn) ,总复杂度是 O(nlog2n)

代码

#include <cstdio>
#include <cstring>
#include <algorithm>

const int N = 50000+10;
const int INF = 1e9;

int n,tim;
int num[N],siz[N],top[N],son[N];
int dep[N],tid[N],rank[N],fa[N];
int head[N],to[N<<1],next[N<<1],w[N<<1],edge;

struct Edge {
    int u,v,c;
}tmp[N<<1];

void Init() {
    memset(head,-1,sizeof head);memset(son,-1,sizeof son);
    tim=0;edge=0;
}

void add_edge(int u,int v,int c) {
    to[edge]=v,w[edge]=c,next[edge]=head[u],head[u]=edge++;
    to[edge]=u,w[edge]=c,next[edge]=head[v],head[v]=edge++;
}

void dfs1(int u,int father,int d) {
    dep[u]=d;fa[u]=father;siz[u]=1;
    for(int i=head[u];~i;i=next[i]) {
        int v=to[i];if(v!=father) {
            dfs1(v,u,d+1);siz[u]+=siz[v];
            if(son[u]==-1 || siz[v]>siz[son[u]])
                son[u]=v;
        }
    }
}

void dfs2(int u,int tp) {
    top[u]=tp;tid[u]=++tim;
    rank[tid[u]]=u;
    if(son[u]==-1)return ;
    dfs2(son[u],tp);
    for(int i=head[u];~i;i=next[i]) {
        int v=to[i];
        if(v!=son[u]&&v!=fa[u]) dfs2(v,v);
    }
}

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int maxx[N<<2];

void pushup(int rt) {
    maxx[rt]=std::max(maxx[rt<<1],maxx[rt<<1|1]);
}

void build(int l,int r,int rt) {
    if(l==r) {
        maxx[rt]=num[l];return ;
    }
    int m=(l+r)>>1;
    build(lson);build(rson);
    pushup(rt);
}

void update(int l,int r,int rt,int p,int val) {
    if(l==r) {
        maxx[rt]=val;return ;
    }
    int m=(l+r)>>1;
    if(p<=m) update(lson,p,val);
    else update(rson,p,val);
    pushup(rt);
}

int Query(int l,int r,int rt,int L,int R ) {
    if(L<=l and r<=R) return maxx[rt];
    int m=(l+r)>>1;
    int ret=-INF;
    if(L<=m) ret=std::max(ret,Query(lson,L,R));
    if(R>m) ret=std::max(ret,Query(rson,L,R));
    return ret;
}

void change(int x,int val) {
    if(dep[tmp[x].u]>dep[tmp[x].v])
        update(2,n,1,tid[tmp[x].u],val);
    else update(2,n,1,tid[tmp[x].v],val);
}

int query(int x,int y) {
    int ans=-INF;
    while(top[x]!=top[y]) {
        if(dep[top[x]]<dep[top[y]]) std::swap(x,y);
        ans=std::max(ans,Query(2,n,1,tid[top[x]],tid[x]));
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) std::swap(x,y);
    if(x!=y) ans=std::max(ans,Query(2,n,1,tid[x]+1,tid[y]));
    return ans;
}

int main()
{
    char oper[15];
    int a,b,c,t;
    scanf("%d",&t);
    while(t--)
    {
        Init();
        scanf("%d",&n);
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            tmp[i].u=a;tmp[i].v=b;tmp[i].c=c;
            add_edge(a,b,c);
        }
        dfs1(1,1,1);
        dfs2(1,1);
        for(int i=1;i<n;i++)
        {
            if(dep[tmp[i].u]>dep[tmp[i].v])
                num[tid[tmp[i].u]]=tmp[i].c;
            else
                num[tid[tmp[i].v]]=tmp[i].c;
        }
        build(2,n,1);
        while(1)
        {
            scanf("%s",oper);
            if(oper[0]=='D') break;
            scanf("%d%d",&a,&b);
            if(oper[0]=='Q')
                printf("%d\n",query(a,b));
            else
                change(a,b);
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值