洛谷 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.
输入输出样例
5 103 41 54 25 45 45 43 54 34 31 33 55 41 53 4
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;}
- 洛谷 P3128 [USACO15DEC]最大流Max Flow
- 洛谷P3128 [USACO15DEC]最大流Max Flow
- 洛谷 P3128 [USACO15DEC] 最大流Max Flow
- 洛谷 P3128 [USACO15DEC]最大流Max Flow
- 洛谷P3128 [USACO15DEC]最大流Max Flow
- P3128 [USACO15DEC]最大流Max Flow
- [USACO15DEC]最大流Max Flow
- [USACO15DEC]最大流Max Flow
- [USACO15DEC]最大流Max Flow题解
- [USACO15DEC]最大流
- POJ 1149 最大流/max flow
- 【学术篇】树上差分--洛谷3128最大流Max Flow
- 最大流最小割(max flow min cut)
- Max Flow / Min Cut 最大流最小割算法学习
- 最大流最小割定理(max flow/min cut theory)
- 最大流最小割定理(max flow/min cut theory)
- Grab Cut 源码解读(最大流-最小割, min-cut\max-flow)
- 最大流最小割定理(max flow/min cut theory)
- 非常芬兰
- Gyroscope in smartphone 手机中的陀螺仪传感器
- 数据集(benchmark)、常用数据集的解析(cifar-10、)
- 带标题的RecyclerViewAdapter
- 毕设知识点总结——part2.聚类
- 洛谷 P3128 [USACO15DEC] 最大流Max Flow
- 获取屏幕宽高
- OutOfMemoryError总结
- TCP与UDP
- 函数调用缺少参数列表;请使用“&Student::Printf”创建指向成员的指针 问题解析
- 功率放大器(PA)
- css nodeType
- EF批量删除执行
- [LeetCode]500. Keyboard Row