zoukankan      html  css  js  c++  java
  • [Usaco2015 dec]Max Flow

    题目描述

    给定一棵有N个点的树,所有节点的权值都为0。

    有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。

    请输出K次操作完毕后权值最大的那个点的权值。

    输入格式

    The first line of the input contains N and K.

    The next N-1 lines each contain two integers x and y (x≠y) describing a pipe between stalls x and y.

    The next K lines each contain two integers ss and t describing the endpoint stalls of a path through which milk is being pumped.

    输出格式

    An integer specifying the maximum amount of milk pumped through any stall in the barn.


    第一眼树链剖分。但似乎不需要用这么暴力的算法。

    我们可以用树上差分。怎么差分呢?对于一条s->t的路径,我们可以在s和t的地方把点权+1,然后在lca的地方把点权-1,再在lca的父亲处把点权-1。做完这些以后,我们求一遍树上前缀和,你就会发现原来一条路径上的点权值都加了1,但又没有影响到其它地方(感性地理解一下为什么)。那么前缀和最大的地方就是答案。

    lca用Tarjan求,那么时间复杂度可以做到O(N+M)。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define maxn 50001
    #define maxm 100001
    using namespace std;
     
    struct edge{
        int to,next;
        edge(){}
        edge(const int &_to,const int &_next){ to=_to,next=_next; }
    }e[maxn<<1];
    int head[maxn],k;
     
    struct question{
        int to,lca,next; bool flag;
        question(){}
        question(const int &_to,const int &_next,const bool &_flag){ to=_to,next=_next,flag=_flag; }
    }q[maxm<<1];
    int qhead[maxn],qk;
     
    int sum[maxn],fa[maxn],pre[maxn];
    bool vis[maxn];
    int n,m,ans;
     
    inline int read(){
        register int x(0),f(1); register char c(getchar());
        while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
        while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
        return x*f;
    }
    inline void add(const int &u,const int &v){ e[k]=edge(v,head[u]),head[u]=k++; }
    inline void link(const int &u,const int &v,const bool &f){ q[qk]=question(v,qhead[u],f),qhead[u]=qk++; }
     
    int get(int x){ return x==fa[x]?x:fa[x]=get(fa[x]); }
    void tarjan(int u){
        vis[u]=true,fa[u]=u;
        for(register int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(vis[v]) continue;
            pre[v]=u,tarjan(v),fa[v]=u;
        }
        for(register int i=qhead[u];~i;i=q[i].next){
            int v=q[i].to;
            if(vis[v]&&!q[i].flag){
                q[i].flag=q[i^1].flag=true,q[i].lca=q[i^1].lca=get(v);
                sum[u]++,sum[v]++,sum[get(v)]--,sum[pre[get(v)]]--;
            }
        }
    }
     
    void dfs_getans(int u){
        for(register int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(v==pre[u]) continue;
            dfs_getans(v);
            sum[u]+=sum[v];
        }
        ans=max(ans,sum[u]);
    }
     
    int main(){
        memset(head,-1,sizeof head),memset(qhead,-1,sizeof qhead);
        n=read(),m=read();
        for(register int i=1;i<n;i++){
            int u=read(),v=read();
            add(u,v),add(v,u);
        }
        for(register int i=1;i<=m;i++){
            int u=read(),v=read();
            link(u,v,false),link(v,u,false);
        }
     
        tarjan(1);
        dfs_getans(1);
        printf("%d
    ",ans);
        return 0;
    }
    

    再摆一份树剖的代码(多半会超时):

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define maxn 50001
    using namespace std;
    
    struct edge{
        int to,next;
        edge(){}
        edge(const int &_to,const int &_next){
            to=_to,next=_next;
        }
    }e[maxn<<1];
    int head[maxn],k;
    
    struct node{
        int l,r,c,f;
    }t[maxn<<2];
    
    inline int read(){
        register int x(0),f(1); register char c(getchar());
        while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
        while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
        return x;
    }
    
    int size[maxn],dep[maxn],son[maxn],fa[maxn];
    int top[maxn],dfn[maxn],id[maxn],tot;
    int n,m;
    
    inline void add(const int &u,const int &v){
        e[k]=edge(v,head[u]);
        head[u]=k++;
    }
    
    void dfs_getson(int u){
        size[u]=1;
        for(register int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(v==fa[u]) continue;
            fa[v]=u,dep[v]=dep[u]+1;
            dfs_getson(v);
            size[u]+=size[v];
            if(size[v]>size[son[u]]) son[u]=v;
        }
    }
    
    void dfs_rewrite(int u,int tp){
        top[u]=tp,dfn[u]=++tot,id[tot]=u;
        if(son[u]) dfs_rewrite(son[u],tp);
        for(register int i=head[u];~i;i=e[i].next){
            int v=e[i].to;
            if(v!=son[u]&&v!=fa[u]) dfs_rewrite(v,v);
        }
    }
    
    void build(int d,int l,int r){
        t[d].l=l,t[d].r=r;
        if(l==r) return;
        int mid=l+r>>1;
        build(d<<1,l,mid),build(d<<1|1,mid+1,r);
    }
    
    inline void down(const int &d){
        t[d<<1].c+=t[d].f*(t[d<<1].r-t[d<<1].l+1);
        t[d<<1|1].c+=t[d].f*(t[d<<1|1].r-t[d<<1|1].l+1);
        t[d<<1].f+=t[d].f;
        t[d<<1|1].f+=t[d].f;
        t[d].f=0;
    }
    
    void change(int d,const int &l,const int &r){
        if(l<=t[d].l&&t[d].r<=r){ t[d].c+=t[d].r-t[d].r+1,t[d].f++; return; }
        if(t[d].f) down(d);
        int mid=t[d].l+t[d].r>>1;
        if(l<=mid) change(d<<1,l,r);
        if(r>mid) change(d<<1|1,l,r);
        t[d].c=t[d<<1].c+t[d<<1|1].c;
    }
    
    int find(int d,const int &x){
        if(t[d].l==t[d].r) return t[d].c;
        if(t[d].f) down(d);
        int mid=t[d].l+t[d].r>>1;
        if(x<=mid) return find(d<<1,x);
        else return find(d<<1|1,x);
    }
    
    int main(){
        memset(head,-1,sizeof head);
        n=read(),m=read();
        for(register int i=1;i<n;i++){
            int u=read(),v=read();
            add(u,v),add(v,u);
        }
        dfs_getson(1);
        dfs_rewrite(1,1);
        build(1,1,tot);
        
        for(register int i=1;i<=m;i++){
            int u=read(),v=read();
            while(top[u]!=top[v]){
                if(dep[top[u]]>dep[top[v]]) swap(u,v);
                change(1,dfn[top[v]],dfn[v]);
                v=fa[top[v]];
            }
            if(dep[u]>dep[v]) swap(u,v);
            change(1,dfn[u],dfn[v]);
        }
        
        int ans=0;
        for(int i=1;i<=n;i++) ans=max(ans,find(1,dfn[i]));
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    javascript中的console.log有什么作用?
    在线js调试工具JSbin、jsFiddle
    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子
    处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
    checkbox的readonly不起作用的解决方案
    jquery-easyui combobox combogrid 级联不可编辑实例
    表格行的全选与单选
    表格与ckeckbox的全选与单选
    隐藏与显示铵钮
    判断字符是否包含有特殊字符
  • 原文地址:https://www.cnblogs.com/akura/p/10945646.html
Copyright © 2011-2022 走看看