spoj 375 Query on a tree(树链剖分模版)

来源:互联网 发布:西科软件招聘 编辑:程序博客网 时间:2024/04/29 07:04

Query on a tree

Time Limit: 5000MS

 Memory Limit: Unknown 64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

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
    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 integersa b c denotes an edge betweena, b of costc (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: 131 2 12 3 2QUERY 1 2CHANGE 1 3QUERY 1 2DONE Output: 13
 
 
题意:给出一棵树,有两种操作,询问a到b路径上边的最大值、改变某条边的值。
思路:树链剖分模版题。
 
AC代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#define ll long longusing namespace std;const int INF = 1e9;const int maxn = 10005;struct Edge{    int v, next;} et[maxn * 2];int tree[maxn];int n, z, num, root;int d[maxn][3];int eh[maxn], dep[maxn], w[maxn], fa[maxn], top[maxn], son[maxn], siz[maxn];void init(){    root = (n + 1) / 2;    fa[root] = z = dep[root] = num = 0;    memset(siz, 0, sizeof(siz));    memset(tree, 0, sizeof(tree));    memset(eh, -1, sizeof(eh));}void add(int u, int v){    Edge e = {v, eh[u]};    et[num] = e;    eh[u] = num++;}void dfs(int u){    siz[u] = 1;    son[u] = 0;    for(int i = eh[u]; i != -1; i = et[i].next)    {        int v = et[i].v;        if(v == fa[u]) continue;        fa[v] = u;        dep[v] = dep[u] + 1;        dfs(v);        if(siz[v] > siz[son[u]]) son[u] = v;        siz[u] += siz[v];    }}void build_tree(int u, int tp){    w[u] = ++z;    top[u] = tp;    if(son[u]) build_tree(son[u], top[u]);    for(int i = eh[u]; i != -1; i = et[i].next)    {        int v = et[i].v;        if(v != son[u] && v != fa[u]) build_tree(v, v);    }}void update(int rt, int l, int r, int loc, int x){    if(loc > r || l > loc) return;    if(l == r)    {        tree[rt] = x;        return;    }    int mid = (l + r) >> 1, ls = rt * 2, rs = ls + 1;    update(ls, l, mid, loc, x);    update(rs, mid + 1, r, loc, x);    tree[rt] = max(tree[ls], tree[rs]);}int maxi(int rt, int left, int right, int l, int r){    if(l > right || r < left) return 0;    if(l <= left && right <= r) return tree[rt];    int mid = (left + right) >> 1, ls = rt * 2, rs = ls + 1;    return max(maxi(ls, left, mid, l, r), maxi(rs, mid + 1, right, l, r));}inline int find(int u, int v){    int f1 = top[u], f2 = top[v], tmp = 0;    while(f1 != f2)    {        if(dep[f1] < dep[f2])        {            swap(f1, f2);            swap(u, v);        }        tmp = max(tmp, maxi(1, 1, z, w[f1], w[u]));        u = fa[f1];        f1 = top[u];    }    if(u == v) return tmp;    if(dep[u] > dep[v]) swap(u, v);    return max(tmp, maxi(1, 1, z, w[son[u]], w[v]));}int main(){    int t, a, b, c;    char str[10];    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        init();        for(int i = 1; i < n; i++)        {            scanf("%d%d%d", &a, &b, &c);            d[i][0] = a, d[i][1] = b, d[i][2] = c;            add(a, b);            add(b, a);        }        dfs(root);        build_tree(root, root);        for(int i = 1; i < n; i++)        {            if(dep[d[i][0]] > dep[d[i][1]]) swap(d[i][0], d[i][1]);            update(1, 1, z, w[d[i][1]], d[i][2]);        }        while(scanf("%s", str))        {            if(strcmp(str, "DONE") == 0) break;            scanf("%d%d", &a, &b);            if(str[0] == 'Q') printf("%d\n", find(a, b));            else update(1, 1, z, w[d[a][1]], b);        }    }    return 0;}

另一模版
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#include <algorithm>#include <queue>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int maxn = 10005;const int INF = 1e9;struct Edge{    int v, next;} et[maxn * 2];int eh[maxn], top[maxn], fa[maxn], dep[maxn], num[maxn], p[maxn], fp[maxn], son[maxn];int tot, pos, n;void init(){    memset(eh, -1, sizeof(eh));    memset(son, -1, sizeof(son));    tot = pos = 0;}void add(int u, int v){    Edge e = {v, eh[u]};    et[tot] = e;    eh[u] = tot++;}void dfs(int u, int pre, int d){    dep[u] = d;    fa[u] = pre;    num[u] = 1;    for(int i = eh[u]; i != -1; i = et[i].next)    {        int v = et[i].v;        if(v != pre)        {            dfs(v, u, d + 1);            num[u] += num[v];            if(son[u] == -1 || num[v] > num[son[u]]) son[u] = v;        }    }}void getpos(int u,int sp){    top[u] = sp;    if(son[u] != -1)    {        p[u] = pos++;        fp[p[u]] = u;        getpos(son[u],sp);    }    else    {        p[u] = pos++;        fp[p[u]] = u;        return;    }    for(int i = eh[u]; i != -1; i = et[i].next)    {        int v = et[i].v;        if(v != son[u] && v != fa[u])            getpos(v,v);    }}struct node{    int l, r, Max;} tree[maxn * 4];void push_up(int rt){    tree[rt].Max = max(tree[L(rt)].Max, tree[R(rt)].Max);}void build(int l, int r, int rt){    tree[rt].l = l, tree[rt].r = r, tree[rt].Max = 0;    if(l == r) return;    int mid = (l + r) >> 1;    build(l, mid, L(rt));    build(mid + 1, r, R(rt));}void update(int k, int val, int rt){    if(tree[rt].l == k && tree[rt].r == k)    {        tree[rt].Max = val;        return;    }    int mid = (tree[rt].l + tree[rt].r) >> 1;    if(k <= mid) update(k, val, L(rt));    else update(k, val, R(rt));    push_up(rt);}int query(int l, int r, int rt){    if(tree[rt].l == l && tree[rt].r == r) return tree[rt].Max;    if(r <= tree[L(rt)].r) return query(l, r, L(rt));    else if(l >= tree[R(rt)].l) return query(l, r, R(rt));    else return max(query(l, tree[L(rt)].r, L(rt)), query(tree[R(rt)].l, r, R(rt)));}int find(int u,int v){    int f1 = top[u], f2 = top[v];    int tmp = 0;    while(f1 != f2)    {        if(dep[f1] < dep[f2])        {            swap(f1,f2);            swap(u,v);        }        tmp = max(tmp,query(p[f1],p[u], 1));        u = fa[f1];        f1 = top[u];    }    if(u == v)return tmp;    if(dep[u] > dep[v]) swap(u,v);    return max(tmp,query(p[son[u]],p[v], 1));}int e[maxn][3];int main(){    char op[10];    int t, u, v;    scanf("%d", &t);    while(t--)    {        init();        scanf("%d", &n);        for(int i = 0; i < n - 1; i++)        {            scanf("%d%d%d", &e[i][0], &e[i][1], &e[i][2]);            add(e[i][0], e[i][1]);            add(e[i][1], e[i][0]);        }        dfs(1, -1, 0);        getpos(1, 1);        build(1, pos, 1);        for(int i = 0; i < n - 1; i++)        {            if(dep[e[i][0]] > dep[e[i][1]])                swap(e[i][0],e[i][1]);            update(p[e[i][1]], e[i][2], 1);        }        while(scanf("%s", op) == 1)        {            if(op[0] == 'D') break;            scanf("%d%d", &u,&v);            if(op[0] == 'Q')                printf("%d\n", find(u,v));            else update(p[e[u-1][1]], v, 1);        }    }    return 0;}


0 0
原创粉丝点击