USACO Max Flow
洛谷 P3128 [USACO15DEC]最大流Max Flow
JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow
Description
Farmer John has installed a new system of N−1pipes to transport milk between the ), conveniently numbered. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.
Input
The first line of the input contains Nand.
The next K lines each contain two integers s 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
Source
题目大意:
FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。
FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。
题解:
树链剖分模板类题。
只不过这次变成了区间修改(+1),以及整个区间的最大值维护。
所以我们写线段树的时候一定要注意相关函数的写法,不再是求和的线段树写法了。
我们用一个数组维护线段树节点表示区间的最大值。那么我们在pushdown打lazy标记的时候,就只能+=k,而不是+=(r-l+1)*k 。
原理也很简单,如果修改区间全包含当前区间,那么当前区间的线段树数组维护的最大值都可以加上1,因为它的左儿子和右儿子全部加1,对最终统计答案并没有丝毫影响。
代码:
#include<cstdio>
#include<algorithm>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int maxn=50001;
int n,k,tot,cnt;
int head[maxn],nxt[maxn<<1],to[maxn<<1];
int deep[maxn],size[maxn],son[maxn],fa[maxn];
int top[maxn],id[maxn];
int fuck[maxn<<2],lazy[maxn<<2],sum[maxn<<2];
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
int x=0,f=1;
char ch=nc();
while(ch<48){if(ch=='-') f=-1;ch=nc();}
while(ch>=48) x=x*10+ch-'0',ch=nc();
return x*f;
}
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs1(int x,int f)
{
deep[x]=deep[f]+1;
fa[x]=f;
size[x]=1;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(y==f)
continue;
dfs1(y,x);
size[x]+=size[y];
if(!son[x]||size[y]>size[son[x]])
son[x]=y;
}
}
void dfs2(int x,int t)
{
id[x]=++cnt;
top[x]=t;
if(!son[x])
return;
dfs2(son[x],t);
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(y==fa[x]||y==son[x])
continue;
dfs2(y,y);
}
}
void mark(int pos,int l,int r,int k)
{
sum[pos]+=k;
lazy[pos]+=k;
}
void pushdown(int pos,int l,int r)
{
int mid=(l+r)>>1;
mark(lson,l,mid,lazy[pos]);
mark(rson,mid+1,r,lazy[pos]);
lazy[pos]=0;
}
void update(int pos,int l,int r,int x,int y,int k)
{
int mid=(l+r)>>1;
if(x<=l && r<=y)
{
mark(pos,l,r,k);
return;
}
pushdown(pos,l,r);
if(x<=mid)
update(lson,l,mid,x,y,k);
if(y>mid)
update(rson,mid+1,r,x,y,k);
sum[pos]=max(sum[lson],sum[rson]);
}
void upd_chain(int x,int y,int k)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]])
swap(x,y);
update(1,1,n,id[top[x]],id[x],k);
x=fa[top[x]];
}
if(deep[x]<deep[y])
swap(x,y);
update(1,1,n,id[y],id[x],k);
}
int main()
{
n=read();k=read();
for(int i=1;i<n;i++)
{
int x,y;
x=read();y=read();
add(x,y);
add(y,x);
}
dfs1(1,0);
dfs2(1,1);
while(k--)
{
int x,y;
x=read();y=read();
upd_chain(x,y,1);
}
printf("%d",sum[1]);
return 0;
}
USACO Max Flow的更多相关文章
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- HackerRank "Training the army" - Max Flow
First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...
- Max Flow
Max Flow 题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N st ...
- min cost max flow算法示例
问题描述 给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同).但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案 ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
随机推荐
- 老师傅珍藏多年CAD常用快捷键合集,收藏,工作效率翻倍!
想要熟练操作CAD,做一名出色的CAD绘图员,少不了勤学苦练,还要掌握一些常用的绘图命令以及常用快捷键. 今天就来跟大家分享超全的CAD绘图命令,以及常用快捷键,学会涨工资! 常用快捷键: CTRL快 ...
- .netcore2.1 ef 使用外键关联查询
//实体类 [Table("invoiceinfo", Schema = "obs")] public class invoice { [Key] public ...
- js中关于constructor与prototype的理解
1.①__proto__和constructor属性是对象所独有的:② prototype属性是函数所独有的,因为函数也是一种对象,所以函数也拥有__proto__和constructor属性. 2. ...
- 在服务器的tomcat中部署手机apk项目,浏览器或手机下载不能根据URL下载和安装apk文件
Android的APK包不能下载或安装,需在tomcat的web.xml加入 <mime-mapping> <extension>apk</extensio ...
- PHP switch的写法
switch switch (expression) { case label1: expression = label1 时执行的代码 ; break; case label2: expressio ...
- 如何使用终端默认情况下阻止Mac应用保存到iCloud
当您保存要在Mac上的Pages,Numbers,TextEdit或其他基于云的应用程序中处理的文档时,该保存的默认位置是iCloud.尽管这对某些人或某些文档来说可能是一件好事,但您可能会厌倦每次更 ...
- 第K个语法符号
在第一行我们写上一个 0.接下来的每一行,将前一行中的0替换为01,1替换为10. 给定行数 N 和序数 K,返回第 N 行中第 K个字符.(K从1开始) 例子: 输入: N = 1, K = 1输出 ...
- 数组累计-reduce
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. array.reduce(f ...
- windows系统搭建Python环境
1.首先访问http://www.python.org/download/去下载最新的python版本. 2.安装下载包,一路next. 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...
- Idea中新建yml不显示叶子形状的原因
IntelliJ IDEA 2019.2.4 x64 (版本),不显示叶子形状,导致写配置无法自动提示(自动提示请安装插件)Spring Assistant 先看一下Editor--->File ...