SPOJ Query on a tree (树链剖分)

Query on a tree

5000ms
262144KB
This problem will be judged on SPOJ. Original ID:  QTREE
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                    
  • 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 ab 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
    

    Tags ( Click to see )


    代码:

    #include <bits/stdc++.h>
    #define lc(o) (o<<1)
    #define rc(o) (o<<1|1)
    using namespace std;
    const int maxn = 1e4 + 10;
    
    struct Edge{
        int from,to,dist;
        Edge(int u,int v,int w):from(u),to(v),dist(w){}
    };
    
    vector<int> g[maxn];
    vector<Edge> edges;
    
    void AddEdge(int u,int v,int w)
    {
        int m = edges.size();
        g[u].push_back(m);
        g[v].push_back(m);
        edges.push_back(Edge(u,v,w));
    }
    
    int siz[maxn],son[maxn],fa[maxn],dep[maxn];
    void pre(int u,int p)
    {
        dep[u] = dep[p] + 1;
        siz[u] = 1;
        fa[u] = p;
        son[u] = 0;
        for(vector<int>::iterator it = g[u].begin(); it != g[u].end(); ++it) {
            Edge & e = edges[*it];
            int v = e.to;
            if(v == u) v = e.from;
            if(v==p)continue;
            pre(v,u);
            siz[u] += siz[v];
            if(siz[son[u]] < siz[v]) son[u] = v;
        }
    }
    
    int root[maxn],pos[maxn],seg[maxn<<2],tot;
    
    void built(int u,int t)
    {
        root[u] = t;
        pos[u] = ++tot;
        if(siz[u] == 1) return;
        built(son[u],t);
        for(vector<int>::iterator it = g[u].begin(); it != g[u].end(); ++it) {
            Edge & e = edges[*it];
            int v = e.to;
            if(v == u) v = e.from;
            if(v==fa[u]||v==son[u])continue;
            built(v,v);
        }
    }
    int ql,qr,val;
    void Modify(int o,int L,int R)
    {
        if(L==R) {
            seg[o] = val;
            return;
        }
        int M = (L+R)>>1;
        if(ql<=M) Modify(lc(o),L,M);
        else Modify(rc(o),M+1,R);
        seg[o] = max(seg[lc(o)],seg[rc(o)]);
    }
    int Query(int o,int L,int R)
    {
        if(ql<=L&&qr>=R) return seg[o];
        int M = (L+R)>>1;
        int ans = 0;
        if(ql <= M) ans = Query(lc(o),L,M);
        if(qr > M) ans = max(ans,Query(rc(o),M+1,R));
        return ans;
    }
    int ask(int u,int v)
    {
        int ans = 0;
        while(root[u] != root[v]) {
            if(dep[root[u]] < dep[root[v]]) swap(u,v);
            ql = pos[root[u]];
            qr = pos[u];
            if(ql <= qr) ans = max(ans,Query(1,1,tot));
            u = fa[root[u]];
        }
        if(u==v)return ans;
        ql = pos[u];
        qr = pos[v];
        if(ql > qr) swap(ql,qr);
        ans = max(ans,Query(1,1,tot));
        return ans;
    }
    int main()
    {
        int T;scanf("%d",&T);
        siz[0] = 0;
        while(T--) {
            int n;scanf("%d",&n);
            edges.clear();
            edges.push_back(Edge(0,0,0));
            for(int i = 1; i <= n; ++i) g[i].clear();
            for(int i = 1; i < n; ++i) {
                int u,v,w;scanf("%d%d%d",&u,&v,&w);
                AddEdge(u,v,w);
            }
            pre(1,0);
            tot = 0;
            built(1,1);
            memset(seg,0,sizeof(*seg)*(4*n));
            for(int i = 1; i < n; ++i) {
                Edge & e = edges[i-1];
                if(dep[e.from] > dep[e.to]) swap(e.from,e.to);
                val = e.dist;
                ql = e.to;
                Modify(1,1,tot);
            }
            char op[10];
            int u,v;
            while(scanf("%s",op) == 1)
            {
                if(op[0] == 'D')break;
                scanf("%d%d",&u,&v);
                if(op[0] == 'Q')
                    printf("%d\n",ask(u,v));
                else {
                    ql = pos[edges[u-1].to];
                    val = v;
                    Modify(1,1,tot);
                }
            }
        }
        return 0;
    }





    • 0
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 3
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值