洛谷 P3128 [USACO15DEC] 最大流Max Flow

来源:互联网 发布:splice软件安卓 编辑:程序博客网 时间:2024/05/21 11:01

题目描述

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


























~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lca+树上差分~

刚开始写了个T到飞起的裸lca……后来才知道还有树上差分这种神奇的东西……

树上差分:对于路径x到y,k为lca(x,y),那么c[x]++,c[y]++,c[k]--,c[fa[k]]--,最后每个点的权值为自己以及以下所有节点的c[i]之和。

然后按照这个写就可以了~

注意lca更新fa[i][j]时先循环j,再循环i!


正确做法:

#include<cstdio>#include<iostream>using namespace std;int n,m,x,y,k,c[50001],fi[50001],w[100001],ne[100001],cnt,fa[50001][17],dep[50001],ans;int read(){int totnum=0;char ch=getchar();while(ch<'0' || ch>'9') ch=getchar();while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}return totnum;}void add(int u,int v){w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;w[++cnt]=u;ne[cnt]=fi[v];fi[v]=cnt;}void dfs(int u){for(int i=fi[u];i;i=ne[i])  if(fa[u][0]!=w[i])  {  fa[w[i]][0]=u;dep[w[i]]=dep[u]+1;dfs(w[i]);  }}int lca(int u,int v){if(dep[u]<dep[v]) swap(u,v);for(int i=16;~i;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];if(u==v) return u;for(int i=16;~i;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];return fa[u][0];}int cal(int u){int now=c[u];for(int i=fi[u];i;i=ne[i])  if(fa[u][0]!=w[i]) now+=cal(w[i]);ans=max(ans,now);return now;}int main(){n=read();m=read();for(int i=1;i<n;i++){x=read();y=read();add(x,y);}dep[1]=1;dfs(1);for(int j=1;j<=16;j++)  for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];while(m--){x=read();y=read();k=lca(x,y);c[x]++;c[y]++;c[k]--;c[fa[k][0]]--;}cal(1);printf("%d\n",ans);return 0;}


裸lca(51分):

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,m,x,y,fi[50001],w[100001],ne[100001],fa[50001][17],cnt,dep[50001],du[50001],ans;int read(){int totnum=0;char ch=getchar();while(ch<'0' || ch>'9') ch=getchar();while(ch>='0' && ch<='9') {totnum=(totnum<<1)+(totnum<<3)+ch-'0';ch=getchar();}return totnum;}void add(int u,int v){w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;w[++cnt]=u;ne[cnt]=fi[v];fi[v]=cnt;}void dfs(int u){for(int i=fi[u];i;i=ne[i])  if(fa[u][0]!=w[i])  {  dep[w[i]]=dep[u]+1;fa[w[i]][0]=u;dfs(w[i]);  }}int lca(int u,int v){if(dep[u]<dep[v]) swap(u,v);for(int i=16;~i;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];if(u==v) return u;for(int i=16;~i;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];return fa[u][0];}int main(){n=read();m=read();for(int i=1;i<n;i++){x=read();y=read();add(x,y);}dep[1]=1;dfs(1);for(int j=1;j<=16;j++)  for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];while(m--){x=read();y=read();int k=lca(x,y);du[k]++;ans=max(ans,du[k]);while(x!=k){du[x]++;ans=max(ans,du[x]);x=fa[x][0];}while(y!=k){du[y]++;ans=max(ans,du[y]);y=fa[y][0];}}printf("%d\n",ans);return 0;}


1 0
原创粉丝点击