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提供译文

思路

树链剖分

代码实现

  1. #include<cstdio>
  2. const int maxn=5e4+;
  3. inline int min_(int x,int y){return x<y?x:y;}
  4. inline int max_(int x,int y){return x>y?x:y;}
  5. inline int swap_(int&x,int&y){x^=y,y^=x,x^=y;}
  6. int n,k;
  7. int a,b;
  8. int eh[maxn],hs,et[maxn<<],en[maxn<<];
  9. int pd[maxn],pf[maxn],pws[maxn],psz[maxn],pps,pp[maxn],pt[maxn];
  10. int ts[maxn<<],tf[maxn<<];
  11. void dfs1(int k,int f,int d){
  12. psz[k]=,pd[k]=d,pf[k]=f;
  13. for(int i=eh[k];i;i=en[i])
  14. if(et[i]!=f){
  15. dfs1(et[i],k,d+);
  16. psz[k]+=psz[et[i]];
  17. if(psz[et[i]]>psz[pws[k]]) pws[k]=et[i];
  18. }
  19. }
  20. void dfs2(int k,int t){
  21. pp[k]=++pps,pt[k]=t;
  22. if(pws[k]) dfs2(pws[k],t);
  23. for(int i=eh[k];i;i=en[i])
  24. if(et[i]!=pf[k]&&et[i]!=pws[k])
  25. dfs2(et[i],et[i]);
  26. }
  27. void down(int k){
  28. int ls=k<<,rs=ls|;
  29. ts[ls]+=tf[k],ts[rs]+=tf[k];
  30. tf[ls]+=tf[k],tf[rs]+=tf[k];
  31. tf[k]=;
  32. }
  33. void change(int k,int l,int r,int al,int ar){
  34. if(l==al&&r==ar){ts[k]++,tf[k]++;return;}
  35. if(tf[k]) down(k);
  36. int mid=l+r>>,ls=k<<,rs=ls|;
  37. if(al<=mid) change(ls,l,mid,al,min_(ar,mid));
  38. if(ar>mid) change(rs,mid+,r,max_(al,mid+),ar);
  39. ts[k]=max_(ts[ls],ts[rs]);
  40. }
  41. int main(){
  42. scanf("%d%d",&n,&k);
  43. for(int i=;i<n;i++){
  44. scanf("%d%d",&a,&b);
  45. ++hs,et[hs]=b,en[hs]=eh[a],eh[a]=hs;
  46. ++hs,et[hs]=a,en[hs]=eh[b],eh[b]=hs;
  47. }
  48. dfs1(,,);
  49. dfs2(,);
  50. while(k--){
  51. scanf("%d%d",&a,&b);
  52. while(pt[a]!=pt[b]){
  53. if(pd[pt[a]]<pd[pt[b]]) swap_(a,b);
  54. change(,,n,pp[pt[a]],pp[a]);
  55. a=pf[pt[a]];
  56. }
  57. if(pd[a]<pd[b]) swap_(a,b);
  58. change(,,n,pp[b],pp[a]);
  59. }
  60. printf("%d\n",ts[]);
  61. return ;
  62. }

[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. JAVA的双色球 小程序

    还是挺简单的,功能过于强大. import java.util.Arrays; import java.util.Random; import java.util.Scanner; public cl ...

  2. 386 Lexicographical Numbers 字典序排数

    给定一个整数 n, 返回从 1 到 n 的字典顺序.例如,给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] .请尽可能的优化算法的时间复杂度和空间复杂度. 输入 ...

  3. H5活动的一些事

    ISUX团队镇楼:https://isux.tencent.com/nine-question-of-swipe-html5-page.html IE6.7.8支持html5新元素 : http:// ...

  4. C#模拟百度登录并到指定网站评论回帖(四)

    基本的实现功能前面已经全部讲完,基本上可以复制黏贴完成登录百度的过程了 今天的这一贴就说说怎么获取百度的验证码 内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回2个值吗?是的,就是codeT ...

  5. jQuery伪分页效果

    如图,我们首先分析在一个页面存放4条内容,其余的超出隐藏(因为这里没有后台数据,所以我们把内容‘写死’),然后就是下面两个按钮(这里我们不用button,因为button有自带的提交功能),然后我们可 ...

  6. Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建;学习Python语言,利用Python语言来写测试用例。加油!!!

    Python+selenium测试环境成功搭建,简单控制浏览器(firefox)接下来,继续学习其他浏览器上的测试环境搭建:学习Python语言,利用Python语言来写测试用例.加油!!!

  7. TensorFlow车牌识别实践(1)

    本文对公开的文章进行验证,从环境搭建到运行到结果分析. 1,文章:基于TensorFlow的车牌号识别系统 文章(译文) http://www.cnblogs.com/Jsmile2017/p/680 ...

  8. tomcat日志详释

    1.tomcat的日志分类: 一是运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息 . 二是访问日志信息,它记录的访问的时间,IP ,访问的资料等相关信息. 2.tomcat的日志目 ...

  9. Angular——自定义过滤器

    基本介绍 除了使用AngularJS内建过滤器外,还可以根业务需要自定义过滤器,通过模块对象实例提供的filter方法自定义过滤器. 基本使用 (1)input是将绑定的数据以参数的形式传入 (2)i ...

  10. 自学php【二】 PHP计算时间加一天

    最近几天在做一个项目,主要是将SQLserver数据到MySQL数据库,一个url跑一次 同步一次昨天的数据,由于很多数据需要同步,所以做了一个操作界面的,一个单纯跑url的 在其中涉及到了对于时间的 ...