SPOJ 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

题意:给你一棵树,两个操作,改变路径权值和查询路径最大值;
分析:
路径最大值可以LCA,但是动态更改路径权值,用到树链剖分,其实就是LCA+DFS序+线段树;

#include <cstdio>  
#include <cstring>  
#include <vector>  
#include <algorithm>  
using namespace std;

const int MAXN = 1e4 + 10;
vector<int> G[MAXN];
int dep[MAXN];//深度
int siz[MAXN];//子节点总数
int fa[MAXN];//父亲节点
int id[MAXN];//DFS序
int son[MAXN];//重儿子
int val[MAXN];//新序列的权值
int top[MAXN];//当前节点的所在链的顶端节点
int num;

struct node {
    int x, y;
    int val;
}e[MAXN];

inline void dfs1(int u, int f, int d) {//记录所有的重边
    dep[u] = d;
    siz[u] = 1;
    son[u] = 0;
    fa[u] = f;
    for(int i = 0; i < G[u].size(); ++i) {
        int x = G[u][i];
        if(x == f) continue;
        dfs1(x, u, d + 1);
        siz[u] += siz[x];
        if(siz[son[u]] < siz[x])
            son[u] = x;
    }
}

inline void dfs2(int u, int tp) {//连重边成重链
    top[u] = tp;
    id[u] = ++num;
    if(son[u]) dfs2(son[u], tp);
    for(int i = 0; i < G[u].size(); ++i) {
        int x = G[u][i];
        if(x == fa[u] || x == son[u]) continue;
        dfs2(x, x);
    }   
}

struct Tree {//线段树更新和查询
    int l, r;
    int val;
}tree[MAXN << 2];

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

inline void build(int root, int l, int r) {
    tree[root].l = l;
    tree[root].r = r;
    if(l == r) {
        tree[root].val = val[l];
        return ;
    }
    int mid = (l + r) >> 1;
    build(root << 1, l, mid);
    build(root << 1 | 1, mid + 1, r);
    pushup(root);
}

inline void updata(int root, int x, int val) {
    if(tree[root].l == tree[root].r) {
        tree[root].val = val;
        return ;
    }
    int mid = (tree[root].l + tree[root].r) >> 1;
    if(x <= mid) updata(root << 1, x, val);
    else updata(root << 1 | 1, x, val);
    pushup(root); 
}

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

int solve(int u, int v) {//LCA
    int tp1 = top[u], tp2 = top[v];
    int ans = 0;
    while(tp1 != tp2) {
        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() {
    int T, n;
    scanf("%d", &T);
    while(T--) {
        for(int i = 0; i < MAXN; ++i) G[i].clear();
        scanf("%d", &n);
        for(int i = 1; i < n; ++i) {
            int x, y, val;
            scanf("%d %d %d", &e[i].x, &e[i].y, &e[i].val);
            G[e[i].x].push_back(e[i].y);
            G[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, 1, num);
        char s[15];
        while(scanf("%s", s) && s[0] != 'D') {
            int x, y;
            scanf("%d %d", &x, &y);
            if(s[0] == 'Q') printf("%d\n", solve(x, y));
            else updata(1, id[e[x].x], y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值