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

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

Platinum鸣谢Claris提供译文

思路

树链剖分

代码实现

 #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的更多相关文章

  1. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

  2. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  3. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

  4. bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)

    题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...

  5. 【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 ...

  6. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

  7. bzoj4393: [Usaco2015 Dec]Fruit Feast

    题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫 ...

  8. USACO Max Flow

    洛谷 P3128 [USACO15DEC]最大流Max Flow 洛谷传送门 JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow JDOJ传送门 Descrip ...

  9. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动

    1# 自定义函数,添加到注册表 Public Sub StartRunRegHKLM() REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Micro ...

  2. 2019 第三届强网杯线上赛部分web复现

    0x00前言 周末打了强网杯,队伍只做得出来6道签到题,web有三道我仔细研究了但是没有最终做出来,赛后有在群里看到其他师傅提供了writeup和环境复现的docker环境,于是跟着学习一波并记录下来 ...

  3. [转]深入C语言内存区域分配(进程的各个段)详解

    一般情况下,一个可执行二进制程序(更确切的说,在Linux操作系统下为一个进程单元,在UC/OSII中被称为任务)在存储(没有调入到内存运行)时拥有3个部分,分别是代码段(text).数据段(data ...

  4. cocos creator 场景如何透明,多个canvas层级显示

    转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...

  5. Java运行报错问题——Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook

    http://blog.csdn.net/xifeijian/article/details/8830933 上述这个朋友博文提醒,可能是因为其他软件添加了JAVA_HOME的路径造成冲突.但他支持删 ...

  6. 关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析

    使用Axis2这个框架进行webservice协议通讯,期间出了个问题,我(CLIENT)请求后,当服务端返回符合协议的SOAP异常报文,例如<soap:fault> ... 我的程序直接 ...

  7. 腾讯云 LNMP+wordpress 搭建个人网站

    折腾了好几个小时才弄好(php nginx略知一二),其实一点都不难! 以此记录一下,献给首次搭建的朋友们!! 1)准备工作:(因为个人用的ubuntu16.04 LTS系统  所以这是debian版 ...

  8. Bootstrap Datatable 简单的基本配置

    $(document).ready(function() {     $('#example').dataTable({ "sScrollX": "100%", ...

  9. HDU_1875_畅通工程再续

    畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  10. 网络编程基础_3.APC队列

    APC队列 #include <stdio.h> #include <windows.h> // 保存 IO 操作的结果 CHAR Buffer1[] = { }; CHAR ...