zoukankan      html  css  js  c++  java
  • [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

    题目描述

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

    FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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 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  to , then it counts as being pumped through the endpoint stalls  and

    , as well as through every stall along the path between them.

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

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

    输入输出格式

    输入格式:

    The first line of the input contains  and .

    The next  lines each contain two integers  and  () describing a pipe

    between stalls  and .

    The next  lines each contain two integers  and  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

    树上差分膜版题
    浪费代码长度

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<iostream>
      4 #include<vector>
      5 using namespace std;
      6 #define inf 0x3f3f3f3f
      7 
      8 inline int read(){
      9     int re=0;
     10     char ch;
     11     bool flag=0;
     12     while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
     13     ch=='-'?flag=1:re=ch-'0';
     14     while((ch=getchar())>='0'&&ch<='9')  re=(re<<1)+(re<<3)+ch-'0';
     15     return flag?-re:re;
     16 }
     17 
     18 struct edge{
     19     int to,next;
     20     edge(int to=0,int next=0):
     21         to(to),next(next){}
     22 };
     23 
     24 struct ask{
     25     int ss,tt,lca;
     26     ask(int ss=0,int tt=0,int lca=0):
     27         ss(ss),tt(tt),lca(lca){}
     28 };
     29 
     30 const int maxn=50001;
     31 
     32 vector<edge> edges;
     33 vector<edge> tree;
     34 vector<edge> ques;
     35 vector<ask> qu;
     36 int n,q,cnt,root=1,ans=-inf;
     37 int head[maxn],tmp_head[maxn],had[maxn],fat[maxn];
     38 int par[maxn],sum[maxn];
     39 bool vis[maxn];
     40 
     41 inline void add_edge(int from,int to){
     42     edges.push_back(edge(to,head[from]));
     43     head[from]=++cnt;
     44     edges.push_back(edge(from,head[to]));
     45     head[to]=++cnt;
     46 }
     47 
     48 inline void add_tree(int from,int to){
     49     tree.push_back(edge(to,tmp_head[from]));
     50     tmp_head[from]=++cnt;
     51 }
     52 
     53 void make_tree(int x,int fa){
     54     fat[x]=fa;
     55     for(int ee=head[x];ee;ee=edges[ee].next)
     56         if(edges[ee].to!=fa){
     57             add_tree(x,edges[ee].to);
     58             make_tree(edges[ee].to,x);
     59         }
     60 }
     61 
     62 inline void add_ques(int ss,int tt){
     63     ques.push_back(edge(tt,had[ss]));
     64     had[ss]=++cnt;
     65     ques.push_back(edge(ss,had[tt]));
     66     had[tt]=++cnt;
     67 }
     68 
     69 void init(){
     70     n=read();  q=read();
     71     edges.push_back(edge(0,0));
     72     cnt=0;
     73     for(int i=1;i<n;i++){
     74         int from=read(),to=read();
     75         add_edge(from,to);
     76     }
     77     
     78     cnt=0;
     79     tree.push_back(edge(0,0));
     80     make_tree(root,0);
     81     swap(head,tmp_head);
     82     
     83     cnt=0;
     84     ques.push_back(edge(0,0));
     85     for(int i=0;i<q;i++){
     86         int ss=read(),tt=read();
     87         qu.push_back(ask(ss,tt,0));
     88         add_ques(ss,tt);
     89     }
     90 }
     91 
     92 int find(int x){
     93     return par[x]==x?x:par[x]=find(par[x]);
     94 }
     95 
     96 void tarjan(int x){
     97     for(int ee=head[x];ee;ee=tree[ee].next){
     98         tarjan(tree[ee].to);
     99         par[tree[ee].to]=x;
    100         vis[tree[ee].to]=1;
    101     }
    102     for(int ee=had[x];ee;ee=ques[ee].next)
    103         if(vis[ques[ee].to])
    104             qu[(ee-1)>>1].lca=find(ques[ee].to);
    105 }
    106 
    107 void dfs_sum(int x){
    108     for(int ee=head[x];ee;ee=tree[ee].next){
    109         dfs_sum(tree[ee].to);
    110         sum[x]+=sum[tree[ee].to];
    111     }
    112 }
    113 
    114 void solve(){
    115     for(int i=1;i<=n;i++)  par[i]=i;
    116     vis[root]=1;
    117     tarjan(root);
    118     
    119     for(int i=0;i<q;i++){
    120         ask qq=qu[i];
    121         sum[qq.ss]++;
    122         sum[qq.tt]++;
    123         sum[qq.lca]--;
    124         sum[fat[qq.lca]]--;
    125     }
    126     dfs_sum(root);
    127     
    128     for(int i=1;i<=n;i++)
    129         ans=max(ans,sum[i]);
    130     printf("%d
    ",ans);
    131 }
    132 
    133 int main(){
    134     //freopen("temp.in","r",stdin);
    135     init();
    136     solve();
    137     return 0;
    138 }

    Goodbye, my almost lover
    再见了,我无缘的爱人
    Goodbye, my hopeless dream
    再见了,我无望的梦想

  • 相关阅读:
    课程作业一
    关于代码中的抄袭(不针对任何人)
    第四次作业
    第三次寒假作业-随笔汇总
    第三次寒假作业-合作
    第三次寒假作业-个人
    第二次寒假作业汇总
    问题
    第二次寒假作业——自学安排
    第二次寒假作业
  • 原文地址:https://www.cnblogs.com/ZYBGMZL/p/6899125.html
Copyright © 2011-2022 走看看