树链剖分(spoj375)基于边权

来源:互联网 发布:super 在java中的用途 编辑:程序博客网 时间:2024/05/16 16:24

原来以为树链剖分是很高端的数据结构,学习之后才知道,树链剖分思想也不是很难,首先把树分成好多条链,然后结合线段树或者伸展树

SPOJ Problem Set (classical)

375. Query on a tree

Problem code: QTREE

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 2DONEOutput:13


思路:把路径映射到线段树上,修改和查询就很简单了,具体上一篇博客讲了。模板是kuangbin大神的

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;struct Edge{    int v,next;}edge[maxn*2];int head[maxn],tot;int top[maxn];//表示v所在的链的顶端节点int fa[maxn];//父亲节点int deep[maxn];//节点深度int num[maxn];//以v为根的字数的节点数(包括自身)int w[maxn];//表示v及其父节点的连边在线段树中的位置int fw[maxn];//线段树种的这个位置是那个节点与其父节点的连边,与w[]正好相反int son[maxn];//重儿子int pos;//线段树中的位置int N;void init(){    tot=0;    memset(head,-1,sizeof(head));    pos=0;    memset(son,-1,sizeof(son));}void add_edge(int u,int v){    edge[tot].v=v;    edge[tot].next=head[u];    head[u]=tot++;}void dfs1(int u,int pre,int d){    fa[u]=pre;    deep[u]=d;    num[u]=1;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(v==pre)continue;        dfs1(v,u,d+1);        num[u]+=num[v];        if(son[u]==-1||num[v]>num[son[u]])            son[u]=v;    }}void dfs2(int u,int sp){    top[u]=sp;    if(son[u]!=-1)    {        w[u]=pos++;        fw[w[u]]=u;        dfs2(son[u],sp);    }    else    {        w[u]=pos++;        fw[w[u]]=u;        return;    }    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].v;        if(v!=son[u]&&v!=fa[u])            dfs2(v,v);    }}struct IntervalTree{    int maxv[maxn<<2];    void build(int o,int l,int r)    {        maxv[o]=0;        if(l==r)return ;        int mid=(l+r)>>1;        build(o<<1,l,mid);        build(o<<1|1,mid+1,r);    }    void pushup(int o)    {        maxv[o]=max(maxv[o<<1],maxv[o<<1|1]);    }    void update(int o,int l,int r,int pos,int val)    {        if(l==r)        {            maxv[o]=val;            return ;        }        int mid=(l+r)>>1;        if(pos<=mid)update(o<<1,l,mid,pos,val);        else update(o<<1|1,mid+1,r,pos,val);        pushup(o);    }    int find(int u,int v)    {        int f1=top[u],f2=top[v];        int tmp=0;        while(f1!=f2)        {            if(deep[f1]<deep[f2])            {                swap(f1,f2);                swap(u,v);            }            tmp=max(tmp,query(1,0,pos-1,w[f1],w[u]));            u=fa[f1],f1=top[u];        }        if(u==v)return tmp;        if(deep[u]>deep[v])swap(u,v);        return max(tmp,query(1,0,pos-1,w[son[u]],w[v]));    }    int query(int o,int l,int r,int q1,int q2)    {        if(q1<=l&&r<=q2)return maxv[o];        if(l==r)return maxv[o];        int mid=(l+r)>>1;        int ans=0;        if(q1<=mid)ans=max(ans,query(o<<1,l,mid,q1,q2));        if(q2>mid)ans=max(ans,query(o<<1|1,mid+1,r,q1,q2));        return ans;    }}tree;int e[maxn][3];int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&N);        init();        for(int i=0;i<N-1;i++)        {            scanf("%d%d%d",&e[i][0],&e[i][1],&e[i][2]);            add_edge(e[i][0],e[i][1]);            add_edge(e[i][1],e[i][0]);        }        dfs1(1,0,0);        dfs2(1,1);        tree.build(1,0,pos-1);        for(int i=0;i<N-1;i++)        {            if(deep[e[i][0]]>deep[e[i][1]])                swap(e[i][0],e[i][1]);            tree.update(1,0,pos-1,w[e[i][1]],e[i][2]);        }        char op[10];        int u,v;        while(scanf("%s",op)!=EOF,op[0]!='D')        {            scanf("%d%d",&u,&v);            if(op[0]=='Q')                printf("%d\n",tree.find(u,v));            else                tree.update(1,0,pos-1,w[e[u-1][1]],v);        }    }    return 0;}





0 0
原创粉丝点击