首页 > 代码库 > 洛谷——P3128 [USACO15DEC]最大流Max Flow

洛谷——P3128 [USACO15DEC]最大流Max Flow

https://www.luogu.org/problem/show?pid=3128

题目描述

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 103 41 54 25 45 45 43 54 34 31 33 55 41 53 4
输出样例#1:
9

 

树剖+树上查分

 1 #include <algorithm> 2 #include <cstdio> 3  4 using namespace std; 5  6 const int N(50000+15); 7 int n,k,u,v; 8  9 int head[N],sumedge;10 struct Edge11 {12     int v,next;13     Edge(int v=0,int next=0):14         v(v),next(next){}15 }edge[N<<1];16 void ins(int u,int v)17 {18     edge[++sumedge]=Edge(v,head[u]);19     head[u]=sumedge;20 }21 22 int size[N],deep[N],dad[N],top[N];23 void DFS(int x)24 {25     size[x]=1; deep[x]=deep[dad[x]]+1;26     for(int i=head[x];i;i=edge[i].next)27     {28         int v=edge[i].v;29         if(dad[x]==v) continue;30         dad[v]=x; DFS(v); size[x]+=size[v];31     }32 }33 void DFS_(int x)34 {35     int t=0; if(!top[x]) top[x]=x;36     for(int i=head[x];i;i=edge[i].next)37     {38         int v=edge[i].v;39         if(dad[x]!=v&&size[t]<size[v]) t=v;40     }41     if(t) top[t]=top[x],DFS_(t);42     for(int i=head[x];i;i=edge[i].next)43     {44         int v=edge[i].v;45         if(dad[x]!=v&&t!=v) DFS_(v);46     }47 }48 int LCA(int x,int y)49 {50     for(;top[x]!=top[y];x=dad[top[x]])51         if(deep[top[x]]<deep[top[y]]) swap(x,y);52     return deep[x]<deep[y]?x:y;53 }54 55 int ans,val[N];56 void DFS_val(int x)57 {58     for(int i=head[x];i;i=edge[i].next)59     {60         int v=edge[i].v;61         if(dad[x]==v) continue;62         DFS_val(v); val[x]+=val[v];63     }64     ans=max(val[x],ans);65 }66 67 int main()68 {69     scanf("%d%d",&n,&k);70     for(int i=1;i<n;i++)71     {72         scanf("%d%d",&u,&v);73         ins(u,v); ins(v,u);74     }75     DFS(1); DFS_(1);76     for(;k--;)77     {78         scanf("%d%d",&u,&v);79         val[u]++; val[v]++;80         int lca=LCA(u,v);81         val[lca]--;val[dad[lca]]--;82     }83     DFS_val(1);84     printf("%d",ans);85     return 0;86 }

 

洛谷——P3128 [USACO15DEC]最大流Max Flow