zoukankan      html  css  js  c++  java
  • 洛谷 P3128 [USACO15DEC]最大流Max Flow

    题目描述

    (FJ)给他的牛棚的(N(2≤N≤50,000))个隔间之间安装了(N-1)根管道,隔间编号从(1)(N)。所有隔间都被管道连通了。

    (FJ)(K(1≤K≤100,000))条运输牛奶的路线,第(i)条路线从隔间(s_i)运输到隔间(t_i)。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

    输入输出格式

    输入格式:

    The first line of the input contains (N) and (K).

    The next (N-1) lines each contain two integers (x) and (y) ((x e y)) describing a pipe

    between stalls (x) and (y).

    The next (K) lines each contain two integers (s) 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.

    输入输出样例

    输入样例#1:

    5 10
    3 4
    1 5
    4 2
    5 4
    5 4
    5 4
    3 5
    4 3
    4 3
    1 3
    3 5
    5 4
    1 5
    3 4
    

    输出样例#1:

    9
    

    思路:树链剖分+线段树,题目中给出的每组点就相当于是路径修改,也就是树链剖分里面的基本操作,要求输出的最大压力值的隔间就是一到n压力值的最大值,因此我们只需要再用线段树维护区间最大值就好了。

    代码:

    #include<cstdio>
    #include<algorithm>
    #include<cctype>
    #define maxn 50007
    #define ls rt<<1
    #define rs rt<<1|1
    using namespace std;
    int id[maxn],d[maxn],n,k,cnt,son[maxn],top[maxn],maxx[maxn<<2];
    int head[maxn],num,fa[maxn],siz[maxn],lazy[maxn<<2];
    inline int qread() {
      char c=getchar();int num=0,f=1;
      for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
      for(;isdigit(c);c=getchar()) num=num*10+c-'0';
      return num*f;
    }
    const int inf=0x7fffffff;
    struct node {
      int v,nxt;
    }e[maxn<<1];
    inline void ct(int u, int v) {
      e[++num].v=v;
      e[num].nxt=head[u];
      head[u]=num;
    }
    void dfs1(int u) {
      siz[u]=1;
      for(int i=head[u];i;i=e[i].nxt) {
        int v=e[i].v;
        if(v!=fa[u]) {
          d[v]=d[u]+1;
          fa[v]=u;
          dfs1(v);
          siz[u]+=siz[v];
          if(siz[v]>siz[son[u]]) son[u]=v;
        }
      }
    }
    void dfs2(int u, int t) {
      id[u]=++cnt;
      top[u]=t;
      if(son[u]) dfs2(son[u],t);
      for(int i=head[u];i;i=e[i].nxt) {
        int v=e[i].v;
        if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
      }
    }
    inline void pushup(int rt) {
      maxx[rt]=max(maxx[ls],maxx[rs]);
    }
    inline void pushdown(int rt) {
      if(lazy[rt]) {
        lazy[ls]+=lazy[rt],maxx[ls]+=lazy[rt];
        lazy[rs]+=lazy[rt],maxx[rs]+=lazy[rt];
        lazy[rt]=0;
      }
    }
    void modify(int rt, int l, int r, int L, int R, int val) {
      if(L>r||R<l) return;
      if(L<=l&&r<=R) {
        lazy[rt]+=val,maxx[rt]+=val;
        return;
      }
      int mid=(l+r)>>1;
      pushdown(rt);
      modify(ls,l,mid,L,R,val),modify(rs,mid+1,r,L,R,val);
      pushup(rt);
    }
    int cmax(int rt, int l, int r, int L, int R) {
      if(L>r||R<l) return -inf;
      if(L<=l&&r<=R) return maxx[rt];
      int mid=(l+r)>>1,ans=-inf;
      pushdown(rt);
      if(L<=mid) ans=max(ans,cmax(ls,l,mid,L,R));
      if(R>mid) ans=max(ans,cmax(rs,mid+1,r,L,R));
      return ans;
    }
    void cal(int x, int y, int val) {
      int fx=top[x],fy=top[y];
      while(fx!=fy) {
        if(d[fx]<d[fy]) swap(x,y),swap(fx,fy);
        modify(1,1,cnt,id[fx],id[x],val);
        x=fa[fx],fx=top[x];
      }
      if(id[x]>id[y]) swap(x,y);
      modify(1,1,cnt,id[x],id[y],val);
    }
    int main() {
      n=qread(),k=qread();
      for(int i=1,u,v;i<n;++i) {
        u=qread(),v=qread();
        ct(u,v),ct(v,u);
      }
      dfs1(1);dfs2(1,1);
      for(int i=1,u,v;i<=k;++i) {
        u=qread(),v=qread();
        cal(u,v,1);
      }
      printf("%d
    ",cmax(1,1,cnt,1,cnt));
      return 0;
    }
    
  • 相关阅读:
    一月5日
    一月5日
    面试java工程师的自我介绍(模拟篇)
    Spring Boot Jpa 介绍
    spring+redis做缓存使用
    Spring Boot Web 开发详解
    thymeleaf+spring的简单搭建
    如何搭建 Spring boot
    通用baseDao
    pagehelper的使用
  • 原文地址:https://www.cnblogs.com/grcyh/p/10226337.html
Copyright © 2011-2022 走看看