[Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 204 Solved: 129
[Submit][Status][Discuss]
Description
Farmer
John has installed a new system of N−1 pipes to transport milk between
the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…N. Each
pipe connects a pair of stalls, and all stalls are connected to
each-other via paths of pipes.
FJ is pumping milk between KK pairs of stalls (1≤K≤100,000). For the
iith such pair, you are told two stalls sisi and titi, 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
KK 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 sisi to titi, then it counts as being pumped
through the endpoint stalls sisi and titi, as well as through every
stall along the path between them.
给定一棵有N个点的树,所有节点的权值都为0。
有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。
请输出K次操作完毕后权值最大的那个点的权值。
Input
The first line of the input contains NN and KK.
The next N−1 lines each contain two integers x and y (x≠y,x≠y) describing a pipe between stalls x and y.
The next K lines each contain two integers ss 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
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
Source
思路
树链剖分
代码实现
#include<cstdio>
const int maxn=5e4+;
inline int min_(int x,int y){return x<y?x:y;}
inline int max_(int x,int y){return x>y?x:y;}
inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
int n,k;
int a,b;
int eh[maxn],hs,et[maxn<<],en[maxn<<];
int pd[maxn],pf[maxn],pws[maxn],psz[maxn],pps,pp[maxn],pt[maxn];
int ts[maxn<<],tf[maxn<<];
void dfs1(int k,int f,int d){
psz[k]=,pd[k]=d,pf[k]=f;
for(int i=eh[k];i;i=en[i])
if(et[i]!=f){
dfs1(et[i],k,d+);
psz[k]+=psz[et[i]];
if(psz[et[i]]>psz[pws[k]]) pws[k]=et[i];
}
}
void dfs2(int k,int t){
pp[k]=++pps,pt[k]=t;
if(pws[k]) dfs2(pws[k],t);
for(int i=eh[k];i;i=en[i])
if(et[i]!=pf[k]&&et[i]!=pws[k])
dfs2(et[i],et[i]);
}
void down(int k){
int ls=k<<,rs=ls|;
ts[ls]+=tf[k],ts[rs]+=tf[k];
tf[ls]+=tf[k],tf[rs]+=tf[k];
tf[k]=;
}
void change(int k,int l,int r,int al,int ar){
if(l==al&&r==ar){ts[k]++,tf[k]++;return;}
if(tf[k]) down(k);
int mid=l+r>>,ls=k<<,rs=ls|;
if(al<=mid) change(ls,l,mid,al,min_(ar,mid));
if(ar>mid) change(rs,mid+,r,max_(al,mid+),ar);
ts[k]=max_(ts[ls],ts[rs]);
}
int main(){
scanf("%d%d",&n,&k);
for(int i=;i<n;i++){
scanf("%d%d",&a,&b);
++hs,et[hs]=b,en[hs]=eh[a],eh[a]=hs;
++hs,et[hs]=a,en[hs]=eh[b],eh[b]=hs;
}
dfs1(,,);
dfs2(,);
while(k--){
scanf("%d%d",&a,&b);
while(pt[a]!=pt[b]){
if(pd[pt[a]]<pd[pt[b]]) swap_(a,b);
change(,,n,pp[pt[a]],pp[a]);
a=pf[pt[a]];
}
if(pd[a]<pd[b]) swap_(a,b);
change(,,n,pp[b],pp[a]);
}
printf("%d\n",ts[]);
return ;
}
[Usaco2015 dec]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 ...
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)
题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...
- 【bzoj4390】[Usaco2015 dec]Max Flow LCA
题目描述 Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in h ...
- 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)
[BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...
- bzoj4393: [Usaco2015 Dec]Fruit Feast
题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫 ...
- USACO Max Flow
洛谷 P3128 [USACO15DEC]最大流Max Flow 洛谷传送门 JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow JDOJ传送门 Descrip ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
随机推荐
- Uva 796 Critical Links (割边+排序)
题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...
- jQuery图片区域选择控件_imgAreaSelect
软考报名时发现可以进行头像区域裁剪功能,F12了一下,发现使用了imgAreaSelect控件. 控件官网: http://odyniec.net/projects/imgareaselect/ 控件 ...
- RabbitMQ三:Rabbit的安装
本章文章,摘自 园友 章为忠 的文章,查找了很多资料,他总结的最细,最全面,我就直接拿过来了 他的原文 http://www.cnblogs.com/zhangweizhong/p/5689209.h ...
- Windows下重置MySQL密码【MYSQL】
使用环境:win10 x64企业版 关闭正在运行的MySQL服务. 打开DOS窗口,转到mysqlbin目录. 输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就 ...
- Spring.Net学习笔记(0)-错误汇总
1.错误一:ObjectDefinitionStoreException "Spring.Objects.Factory.ObjectDefinitionStoreException&quo ...
- RabbitMQ指南之五:主题交换器(Topic Exchange)
在上一章中,我们完善了我们的日志系统,用direct交换器替换了fanout交换器,使得我们可以有选择性地接收消息.尽管如此,仍然还有限制:不能基于多个标准进行路由.在我们的日志系统中,我们可能不仅希 ...
- 实用and常用shell命令汇编
很久没写blog了,基本都在用 github和笔记.现在将一些常用的shell并且很使用的shell用法分享一下: 分行读取,切割,计数: cat product.txt | while read l ...
- 如何删除sublime目录
左侧栏的sublime目录一直删不掉,删除列直接变成了灰色. 今天才发现应该选择文件夹右击选择工程——从工程中删除文件夹. 这个设计真的很醉,删除这么常用的键还放进了第二层……
- activity生命周期知识点整理
activity生命周期知识点整理 Activity: 是一个应用组件,用户可与其提供的屏幕进行交互.窗口通常会充满屏幕,但也可以小于屏幕并浮动在其他窗口之上. 一个activity的什么周期: 启动 ...
- 配置服务器 Ubuntu 记录+踩坑
从零开始配置服务器用于ss+站点 1. SS 首先安装pyenv,安装pyenv之前先安装必要环境,具体命令行请见: https://github.com/pyenv/pyenv/wiki/Commo ...