[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 ...
随机推荐
- easyui-datebox 年月视图显示
//年月视图做法 $('#startYearDate').datebox({ onShowPanel: function () { //显示日趋选择对象后再触发弹出月份层的事件,初始化时没有生成月份层 ...
- 清理TIME_WAIT
cat >> /etc/sysctl.conf << EOFnet.ipv4.tcp_tw_reuse=1net.ipv4.tcp_tw_recycle=1net.ipv4.t ...
- [GDOI2014]拯救莫莉斯
题目描述 莫莉斯·乔是圣域里一个叱咤风云的人物,他凭借着自身超强的经济头脑,牢牢控制了圣域的石油市场. 圣域的地图可以看成是一个n*m的矩阵.每个整数坐标点(x , y)表示一座城市吗,两座城市间相邻 ...
- spring的依赖注入如何降低了耦合
依赖注入:程序运行过程中,如需另一个对象协作(调用它的方法.访问他的属性时),无须在代码中创建被调用者,而是依赖于外部容器的注入 看过一些比较好的回答 1.一个人(Java实例,调用者)需要一把斧子( ...
- Django--4、认证系统
cookie与session 概念 因http协议无法保存状态,但是又需要保存状态,所以有了cookie.它不属于http协议范畴 工作原理:相当于一段标识数据.在访问服务器产生标识内容(cookie ...
- Apache ab使用指南
Apache ab使用图例: 其中比较重要的两个指标要特别注意: Requests per second:表示平均每秒事务数,相当于LR的TPS Time per second:用户请求平均响应时间和 ...
- python自动化--模块操作之re、MySQL、Excel
一.python自有模块正则 import re # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None print(re.match("www ...
- 2014中秋节,用java为QQ游戏美女找茬写辅助
引子 今年中秋闲在家,总要找点事做. 前几天开始学python,很早之前就有计划拿下这门语言了,可惜一直拖到现在……不可否认,我也是个拖沓症患者.在学习python的过程中 ...
- mysql 性能优化索引、缓存、分表、分布式实现方式。
系统针对5000台终端测试结果 索引 目标:优化查询速度3秒以内 需要优化.尽量避免使用select * 来查询对象.使用到哪些属性值就查询出哪些使用即可 首页页面: 设备-组织查询 优化 避免使用s ...
- 梦想CAD控件 2019.05.05更新
下载地址: http://www.mxdraw.com/ndetail_20141.html 1. 增加vs2017版本控件 2. 增加windows触摸屏支持 3. 增加手写签名功能 4. 修改PL ...