zoukankan      html  css  js  c++  java
  • spoj 375 Query on a tree (树链剖分)

    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
    第一次接触树链剖分,貌似是用来处理对树的边权的多次询问,然后对边权进行编号,转化为节点之间的询问。具体关于树链剖分的解析见 http://blog.csdn.net/acdreamers/article/details/10591443
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <time.h>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    #define lson(x) ((x<<1))
    #define rson(x) ((x<<1)+1)
    using namespace std;
    typedef long long ll;
    const int N=1e5+50;
    const int M=N*N+10;
    int dep[N],siz[N],fa[N],id[N],son[N],val[N],top[N]; //top 最近的重链父节点
    int num;
    vector<int> v[N];
    struct tree {
        int x,y,val;
        void read() {
            scanf("%d%d%d",&x,&y,&val);
        }
    };
    tree e[N];
    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 < v[u].size(); i++) {
            int ff = v[u][i];
            if (ff == f) continue;
            dfs1(ff, u, d + 1);
            siz[u] += siz[ff];
            if (siz[son[u]] < siz[ff])
                son[u] = ff;
        }
    }
    void dfs2(int u, int tp) {
        top[u] = tp;
        id[u] = ++num;
        if (son[u]) dfs2(son[u], tp);
        for (int i = 0; i < v[u].size(); i++) {
            int ff = v[u][i];
            if (ff == fa[u] || ff == son[u]) continue;
            dfs2(ff, ff);
        }
    }
    
    struct Tree {
        int l,r,val;
    };
    Tree tree[4*N];
    void pushup(int x) {
        tree[x].val = max(tree[lson(x)].val, tree[rson(x)].val);
    }
    
    void build(int l,int r,int v) {
        tree[v].l=l;
        tree[v].r=r;
        if(l==r) {
            tree[v].val = val[l];
            return ;
        }
        int mid=(l+r)>>1;
        build(l,mid,v*2);
        build(mid+1,r,v*2+1);
        pushup(v);
    }
    void update(int o,int v,int val) { //log(n)
        if(tree[o].l==tree[o].r) {
            tree[o].val = val;
            return ;
        }
        int mid = (tree[o].l+tree[o].r)/2;
        if(v<=mid)
            update(o*2,v,val);
        else
            update(o*2+1,v,val);
        pushup(o);
    }
    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) / 2;
        int ans = 0;
        if (l <= mid) ans = max(ans, query(lson(x),l,r));
        if (r > mid) ans = max(ans, query(rson(x),l,r));
        return ans;
    }
    
    int Yougth(int u, int v) {
        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;
    }
    void Clear(int n) {
        for(int i=1; i<=n; i++)
            v[i].clear();
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            int n;
            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[200];
            while(~scanf("%s",&s) && s[0]!='D') {
                int x,y;
                scanf("%d%d",&x,&y);
                if(s[0]=='Q')
                    printf("%d
    ",Yougth(x,y));
                if (s[0] == 'C')
                    update(1,id[e[x].x],y);
            }
            Clear(n);
        }
        return 0;
    }

  • 相关阅读:
    C# Console.Read()和Console.ReadLine()的区别
    C#获取项目程序及运行路径的方法
    Thread理解
    ENVI 监督分类Max stdev from Mean 参数IDL中的设置
    C# 插件开发学习实例
    利用动软代码器自动生存三层架构
    结构体的对齐
    链表分类及应用
    指针
    双向循环链表
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6354296.html
Copyright © 2011-2022 走看看