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

    4390: [Usaco2015 dec]Max Flow

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 139  Solved: 85
    [Submit][Status][Discuss]

    Description

    Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…N. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

    FJ is pumping milk between KK pairs of stalls (1≤K≤100,000). For the iith such pair, you are told two stalls sisi and titi, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stalls might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along many of the KK paths along which milk is being pumped. Please help him determine the maximum amount of milk being pumped through any stall. If milk is being pumped along a path from sisi to titi, then it counts as being pumped through the endpoint stalls sisi and titi, as well as through every stall along the path between them.

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

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

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

    Input

    The first line of the input contains NN and KK.

    The next N−1 lines each contain two integers x and y (x≠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.

    Output

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

    Sample Input

    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

    Sample Output

    9

    HINT

     

    Source

    已经忘了树上差分怎么写的...

    给s和t加一 lca(s,t)和lca(s,t)的父亲-1

    然后每个点的值就是子树的权值和

     1 #include<bits/stdc++.h>
     2 #define rep(i,l,r) for(int i=l;i<=r;++i)
     3 using namespace std;
     4 const int N=102333;
     5 int head[N],tot,dep[N],sz[N],fa[N],son[N],top[N],size[N],sum[N],n,Q,a,b,ans,c;
     6 struct node{
     7     int next,to;
     8 }e[N<<1];
     9 inline void ins(int u,int v) {
    10      e[++tot].to=v; e[tot].next=head[u]; head[u]=tot; 
    11 }
    12 inline void dfs1(int x,int pre) {
    13      dep[x]=dep[fa[x]=pre]+1; sz[x]=1;
    14      for(int k=head[x];k;k=e[k].next) {
    15           if(e[k].to==pre)  continue;
    16           dfs1(e[k].to,x);
    17           sz[x]+=sz[e[k].to];
    18           if(sz[e[k].to]>sz[son[x]]) son[x]=e[k].to;
    19      }
    20 }
    21 void dfs2(int x,int tr) {
    22      top[x]=tr; 
    23      if(son[x]) dfs2(son[x],tr);
    24      for(int k=head[x];k;k=e[k].next) {
    25           if(e[k].to==fa[x] || e[k].to==son[x]) continue;
    26           dfs2(e[k].to,e[k].to);
    27      }
    28 }
    29 inline int lca(int x,int y) {
    30      int a,b;
    31      a=top[x]; b=top[y];
    32      while(a!=b) {
    33          if(dep[a]<dep[b]) swap(a,b),swap(x,y);
    34          x=fa[a]; a=top[x];
    35      }
    36      if(dep[x]<dep[y]) return x;else return y;
    37 }
    38 void dfs(int x,int fa){
    39     size[x]=sum[x];
    40     for(int k=head[x];k;k=e[k].next) if(fa!=e[k].to){
    41         dfs(e[k].to,x); size[x]+=size[e[k].to];
    42     }
    43 }
    44 int main(){
    45     scanf("%d%d",&n,&Q);
    46     rep(i,1,n-1) scanf("%d%d",&a,&b),ins(a,b),ins(b,a);
    47     dfs1(1,0); dfs2(1,1);
    48     rep(i,1,Q) {
    49          scanf("%d%d",&a,&b);  c=lca(a,b);
    50          sum[fa[c]]--; sum[a]++; sum[b]++; sum[c]--;
    51     }
    52     dfs(1,0);
    53     rep(i,1,n) ans=max(ans,size[i]);
    54     printf("%d
    ",ans);
    55 }
    View Code
  • 相关阅读:
    信息安全系统设计基础实验三报告
    信息安全系统设计基础第十二周学习总结
    信息安全系统设计基础实验二报告
    信息安全系统设计基础第十一周学习总结
    家庭作业汇总
    信息安全系统设计基础实验一报告
    信息安全系统设计基础第十周学习总结
    第十章家庭作业
    20182319彭淼迪第一周学习总结
    java预备作业
  • 原文地址:https://www.cnblogs.com/Bloodline/p/5854482.html
Copyright © 2011-2022 走看看