码迷,mamicode.com
首页 > 其他好文 > 详细

[SPOJ 375]Query on a tree

时间:2018-02-23 10:45:06      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:com   write   push   owb   dig   for   return   link   链接   

Description

题库链接

给定 \(n\) 个点的树。支持以下操作:

  1. CHANGE \(i\) \(t_i\) 将第 \(i\) 条边权值改为 \(t_i\)
  2. QUERY \(a\) \(b\) 询问从 \(a\) 点到 \(b\) 点路径上的最大边权

多组数据。

\(1\leq n\leq 10000,t\leq 20\)

Solution

\(LCT\) 维护边权信息,把边转换成点就好了。

Code

//It is made by Awson on 2018.2.23
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 2e4;
void read(int &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }

struct Link_Cut_Tree {
    int ch[N+5][2], maxn[N+5], val[N+5], pre[N+5], isrt[N+5], rev[N+5], tot;
    void clear() {for (int o = 1; o <= N; o++) ch[o][0] = ch[o][1] = maxn[o] = val[o] = pre[o] = rev[o] = 0, isrt[o] = 1; tot = 0; }
    void pushup(int o) {maxn[o] = Max(maxn[ch[o][0]], maxn[ch[o][1]]), maxn[o] = Max(maxn[o], val[o]); }
    void pushdown(int o) {
    if (!rev[o]) return; int ls = ch[o][0], rs = ch[o][1];
    Swap(ch[ls][0], ch[ls][1]), Swap(ch[rs][0], ch[rs][1]);
    rev[ls] ^= 1, rev[rs] ^= 1, rev[o] = 0;
    }
    void push(int o) {if (!isrt[o]) push(pre[o]); pushdown(o); }
    void rotate(int o, int kind) {
    int p = pre[o];
    ch[p][!kind] = ch[o][kind], pre[ch[o][kind]] = p;
    if (isrt[p]) isrt[o] = 1, isrt[p] = 0; else ch[pre[p]][ch[pre[p]][1] == p] = o;
    pre[o] = pre[p];
    ch[o][kind] = p, pre[p] = o;
    pushup(p), pushup(o);
    }
    void splay(int o) {
    push(o);
    while (!isrt[o]) {
        if (isrt[pre[o]]) rotate(o, ch[pre[o]][0] == o);
        else {
        int p = pre[o], kind = ch[pre[p]][0] == p;
        if (ch[p][kind] == o) rotate(o, !kind), rotate(o, kind);
        else rotate(p, kind), rotate(o, kind);
        }
    }
    }
    void access(int o) {
    int y = 0;
    while (o) {
        splay(o);
        isrt[ch[o][1]] = 1, isrt[ch[o][1] = y] = 0;
        pushup(o); o = pre[y = o];
    }
    }
    void makeroot(int o) {access(o); splay(o); Swap(ch[o][0], ch[o][1]), rev[o] ^= 1; }
    void split(int u, int v) {makeroot(u); access(v); splay(v); }
    void link(int u, int v) {makeroot(u); pre[u] = v; }
    void cut(int u, int v) {split(u, v); ch[v][0] = pre[u] = 0, isrt[u] = 1; pushup(v); }
    int query(int u, int v) {split(u, v); return maxn[v]; }
    void update(int id, int key) {makeroot(id); val[id] = key; pushup(id); }
}T;

int n, u[N+5], v[N+5], c[N+5], a, b;
char ch[20];

void work() {
    read(n); T.clear();
    for (int i = 1; i < n; i++) {
    read(u[i]), read(v[i]), read(c[i]); T.val[i+n] = T.maxn[i+n] = c[i];
    T.link(u[i], i+n), T.link(v[i], i+n);
    }
    while (~scanf("%s", ch)) {
    if (ch[0] == 'D') return;
    read(a), read(b); 
    if (ch[0] == 'C') T.update(a+n, b); else writeln(a == b ? 0 : T.query(a, b));
    }
}
int main() {
    int t; cin >> t;
    while(t--) work(); return 0;
}

[SPOJ 375]Query on a tree

标签:com   write   push   owb   dig   for   return   link   链接   

原文地址:https://www.cnblogs.com/NaVi-Awson/p/8460749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!