zoukankan      html  css  js  c++  java
  • spoj 375 树链剖分模板

    /*
    只是一道树链刨分的入门题,作为模板用。
    */
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<map>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    using namespace std;
    #define N  11000
    #define inf 0x3fffffff
    int head[N];
    int son[N];//记录与当前点相连的数目最多的子节点的下标
    int fa[N];//记录上一个父节点
    int siz[N];//记录当前节点的子节点的数目
    int top[N];//当前链的最顶层
    int f[N];//重新标记
    int fp[N];//记录重新标记前的点
    int deep[N];//深度
    int w[N];//  记录当前点与其父节点的关系
    int nu,yong;
    int Max;
    struct nodee
    {
        int u,v,w,next;
    } bian[N*4],ff[N];
    void init()
    {
        yong=nu=0;
        memset(head,-1,sizeof(head));
        memset(son,-1,sizeof(son));
    }
    void addedge(int u,int v,int w)
    {
        bian[yong].u=u;
        bian[yong].v=v;
        bian[yong].next=head[u];
        head[u]=yong++;
    }
    void dfs(int u,int father,int d)
    {
        deep[u]=d;//记录深度
        siz[u]=1;//初始化
        fa[u]=father;//记录父节点
        int i;
        for(i=head[u]; i!=-1; i=bian[i].next)
        {
            int v=bian[i].v;
            if(v!=father)
            {
                dfs(v,u,d+1);//
                siz[u]+=siz[v];//回溯时累加数目
                if(son[u]==-1||siz[son[u]]<siz[v])//son[u]记录当前点相连的点的子节点数目最多的点,即重链
                    son[u]=v;//u-v
            }
        }
    }
    void getnu(int u,int cnt)
    {
        top[u]=cnt;//记录当前链的顶端
        f[u]=nu++;//重新标记
        fp[f[u]]=u;//记录标记前的编号
        if(son[u]==-1)return ;//如果他没有儿子节点
        getnu(son[u],cnt);//重链
        int i;
        for(i=head[u]; i!=-1; i=bian[i].next)
        {
            int v=bian[i].v;
            if(v!=fa[u]&&v!=son[u])//排除重链
                getnu(v,v);//轻链
        }
        return;
    }
    //******以上求重链和轻链以及各部分的初始化,下面是线段树部分和查询询问**********//
    struct node
    {
        int l,r,maxx;
    } tree[N*4];
    int Ma(int v,int vv)
    {
        return v>vv?v:vv;
    }
    void pushup(int t) //回溯时更新最大值和最小值
    {
        tree[t].maxx=Ma(tree[t*2].maxx,tree[t*2+1].maxx);
    }
    void build(int t,int l,int r)//建树
    {
        tree[t].l=l;
        tree[t].r=r;
        if(tree[t].l==tree[t].r)
        {
            tree[t].maxx=w[tree[t].l];//记录边权值
            return ;
        }
        int mid=(l+r)>>1;
        build(t*2,l,mid);
        build(t*2+1,mid+1,r);
        pushup(t);
    }
    void qury(int t,int l,int r)//询问区间的最大值
    {
        if(tree[t].l==l&&tree[t].r==r)//如果查到
        {
            Max=Ma(Max,tree[t].maxx);//
            return ;
        }
        int mid=(tree[t].l+tree[t].r)>>1;
        if(r<=mid)
            qury(t*2,l,r);
        else if(l>mid)
            qury(t*2+1,l,r);
        else
        {
            qury(t*2,l,mid);
            qury(t*2+1,mid+1,r);
        }
        pushup(t);
    }
    int findmax(int u,int  v)//查找最大值
    {
        int f1=top[u];//得到顶端编号值
        int f2=top[v];
        int ans=-inf;//初始化最小值
        while(f1!=f2)//结束条件,再通一个重链上
        {
            if(deep[f1]<deep[f2])//从最深层开始网上
            {
                swap(f1,f2);//交换
                swap(u,v);
            }
            Max=-inf;
            qury(1,f[f1],f[u]);//询问重新编号后的f[u]和其顶端节点之间的最大值,从而使其从f[u]跳到顶端
            ans=Ma(ans,Max);//ans储存最大值
            u=fa[f1];//从f1向上跳一步,不管当前链是轻链还是重链
            f1=top[u];//得到跳一步后的顶端节点,继续比较
        }//
        if(v==u)return ans;//如果在同一点就直接返回
        if(deep[u]>deep[v])swap(u,v);//得到u,v之间的最小值
        Max=-inf;
        qury(1,f[son[u]],f[v]);//求出u的子节点的f[v]---f[u的最大数目子节点];,因为此时他们在一个重链上
        ans=Ma(ans,Max);//求出最大值
        return ans;
    }
    void update(int t,int x,int y)//更新
    {
        if(tree[t].l==x&&tree[t].r==x)//
        {
            tree[t].maxx=y;
            return ;
        }
        int mid=(tree[t].l+tree[t].r)/2;
        if(x<=mid)
            update(t*2,x,y);
        else
            update(t*2+1,x,y);
        pushup(t);
    }
    int main()
    {
        int T,i,n;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            init();
            for(i=1; i<n; i++)
            {
                scanf("%d%d%d",&ff[i].u,&ff[i].v,&ff[i].w);
                addedge(ff[i].u,ff[i].v,ff[i].w);
                addedge(ff[i].v,ff[i].u,ff[i].w);
            }
            dfs(1,1,0);//深搜的到 每个节点的深度,父节点和其子节点的数目(包括本身),还有最大数目的子节点的编号
            getnu(1,1);//得到重链或者轻链的顶端和重新编号并记录重新编号前的值。如果是轻链的话
            for(i=1; i<n; i++)
            {
                if(deep[ff[i].u]<deep[ff[i].v])
                    swap(ff[i].u,ff[i].v);//得到深度最大的节点
                w[f[ff[i].u]]=ff[i].w;//记录重新编号后的当前点与上一个点的权值
            }
            build(1,1,nu-1);//建树
            char s[222];
            int x,y;
            while(scanf("%s",s),strcmp(s,"DONE"))
            {
                scanf("%d%d",&x,&y);
                if(s[0]=='Q')
                    printf("%d
    ",findmax(x,y));//找区间最大值
                else
                    update(1,f[ff[x].u],y);//更换区间中某个值,ff[x].u是深度较大的数,所以不会出现越界情况,即f[ff[x].u]不为0
            }
        }
        return 0;
    }
    

  • 相关阅读:
    TestNG DataProvider的几种方法写法
    ruby操作EXCEL的简单示例
    QTP的tsr对象库文件转换成XML
    Ruby对时间的处理
    java读取YAML文件
    ruby遍历文件夹
    ruby操作excel文件
    [转载]利用ruby的Net::HTTP发起http请求并对返回包进行简单的校验
    QTP连接MySQL (转载)
    Ruby 冒泡排序
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410604.html
Copyright © 2011-2022 走看看