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. mac更改本地mysql登陆密码

    安装完mysql 之后,登陆以后,运行任何命令,提示 You must SET PASSWORD before executing this statement解决办法. step 1: SET PA ...

  2. Linux环境下修改MySQL数据库存储引擎

    今天在执行Oracle数据库迁移至MySQL数据库时报出了一个错误信息: Specified key was too bytes 百度发现,原来需要更改MySQL数据库的存储引擎为InnoDB,查询目 ...

  3. 笔记本 windows 10 安装

    开机按快捷键是F12,选择从usb启动.秋叶系统 很好用,推荐使用. 联想笔记本u深度一键u盘启动BIOS设置教程:准备工作:制作好u深度u盘启动盘http://rj.baidu.com/soft/d ...

  4. c++类的内存布局

    问题: 考察了reinterpret_cast和static_cast的区别.顺道发现了一个可以查看c++内存布局的工具(在VS中). 结果: 前两个输出的地址形同,后一个不同. class A{in ...

  5. 274 H-Index H指数

    给定一位研究者的论文被引用次数的数组(被引用次数是非负整数).写一个方法计算出研究者的H指数.H-index定义: “一位科学家有指数 h 是指他(她)的 N 篇论文中至多有 h 篇论文,分别被引用了 ...

  6. mysql若干问题

    一.Host ip is not allowed to connect to this MySql server 解决方法:这是因为你的账号不允许远程登录,只能在localhost.只要在localh ...

  7. Linux学习日记之Deepin下查看crontab运行日志

    Deepin使用 journalctl 替代了 syslog 来处理系统日志 故查看crontab运行日志应使用 journalctl -f /usr/sbin/cron

  8. 动态排序JavaBean

    Java中如果对对象排序可以考虑实现Comparable接口,但是需要排序的属性一旦指定就不能再修改.BeanUtils组件提供了对JavaBean动态排序的支持,即可以在运行时指定排序的属性.实例运 ...

  9. daxcie

    Database->Edit Current DBMS菜单 修改如下:选中General选项卡,依次打开Script->Sql->Fomat->CaseSensitivityU ...

  10. HDU_2112_最短路

    题目链接:http://acm.hdu.edu.cn/status.php?user=l1526789512&pid=2112&status=5 HDU Today Time Limi ...