题目描述

Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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 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  to , then it counts as being pumped through the endpoint stalls  and

, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains  and .

The next  lines each contain two integers  and  () describing a pipe

between stalls  and .

The next  lines each contain two integers  and  describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

输入输出样例

输入样例#1:

  1. 5 10
  2. 3 4
  3. 1 5
  4. 4 2
  5. 5 4
  6. 5 4
  7. 5 4
  8. 3 5
  9. 4 3
  10. 4 3
  11. 1 3
  12. 3 5
  13. 5 4
  14. 1 5
  15. 3 4
输出样例#1:

  1. 9

倍增LCA+树上差分。

树结点的权值等于其子树所有节点的差分结果。

704ms,不知道用树剖求LCA会有多快

  1. /*by SilverN*/
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cstdio>
  6. #include<cmath>
  7. using namespace std;
  8. const int mxn=;
  9. int read(){
  10. int x=,f=;char ch=getchar();
  11. while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
  12. while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
  13. return x*f;
  14. }
  15. struct edge{
  16. int v,nxt;
  17. }e[mxn<<];
  18. int hd[mxn],mct=;
  19. void add_edge(int u,int v){
  20. e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
  21. }
  22. int n,k;
  23. int dep[mxn];
  24. int fa[mxn][];
  25. int a[mxn];//差分
  26. void DFS(int u,int f){
  27. dep[u]=dep[f]+;
  28. for(int i=;i<;i++)fa[u][i]=fa[fa[u][i-]][i-];
  29. for(int i=hd[u];i;i=e[i].nxt){
  30. int v=e[i].v;
  31. if(v==f)continue;
  32. fa[v][]=u;
  33. DFS(v,u);
  34. }
  35. return;
  36. }
  37. int LCA(int x,int y){
  38. if(dep[x]<dep[y])swap(x,y);
  39. for(int i=;i>=;i--){
  40. if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
  41. }
  42. if(x==y)return x;
  43. for(int i=;i>=;i--){
  44. if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
  45. }
  46. return fa[x][];
  47. }
  48. int ans=-1e9;
  49. int clc(int u,int f){
  50. int res=a[u];
  51. for(int i=hd[u];i;i=e[i].nxt){
  52. int v=e[i].v;
  53. if(v==f)continue;
  54. res+=clc(v,u);
  55. }
  56. ans=max(ans,res);
  57. return res;
  58. }
  59. int main(){
  60. n=read();k=read();
  61. int i,j,x,y;
  62. for(i=;i<n;i++){
  63. x=read();y=read();
  64. add_edge(x,y);
  65. add_edge(y,x);
  66. }
  67. DFS(,);
  68. for(i=;i<=k;i++){
  69. x=read();y=read();
  70. a[x]++;a[y]++;
  71. int tmp=LCA(x,y);
  72. a[tmp]--;
  73. if(fa[tmp][])a[fa[tmp][]]--;
  74. }
  75. clc(,);
  76. printf("%d\n",ans);
  77. return ;
  78. }

洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]的更多相关文章

  1. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

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

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

  3. 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...

  4. 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)

    题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...

  5. 洛谷 P3128 [USACO15DEC]最大流Max Flow

    题目描述 \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤10 ...

  6. 洛谷——P3128 [USACO15DEC]最大流Max Flow

    https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to ...

  7. 洛谷P3128 [USACO15DEC]最大流Max Flow (树上差分)

    ###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a  b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...

  8. 题解——洛谷P3128 [USACO15DEC]最大流Max Flow

    裸的树上差分 因为要求点权所以在点上差分即可 #include <cstdio> #include <algorithm> #include <cstring> u ...

  9. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

随机推荐

  1. Java 基础命名空间

    java.lang (提供利用 Java 编程语言进行程序设计的基础类)java.lang.annotation(提供了引用对象类,支持在某种程度上与垃圾回收器之间的交互)java.lang.inst ...

  2. codevs 1051 接龙游戏

    codevs 1051 接龙游戏 http://codevs.cn/problem/1051/ 题目描述 Description 给出了N个单词,已经按长度排好了序.如果某单词i是某单词j的前缀,i- ...

  3. Python之路【第十一篇】前端初识之HTML

    HTML HTML解释: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他 ...

  4. 教你如何反编译Android安装文件apk来偷窥源代码

    本文章首发于浩瀚先森博客,地址:http://www.guohao1206.com/2016/08/23/970.html 1. 准备 - java环境 安装java并配置环境 => JAVA环 ...

  5. C#基础之泛型

    1.泛型的本质 泛型的好处不用多说,在.NET中我看到有很多技术都是以泛型为基础的,不过因为不懂泛型而只能对那些技术一脸茫然.泛型主要用于集合类,最主要的原因是它不需要装箱拆箱且类型安全,比如很常用的 ...

  6. WPF 3D模型 3D场景

    1.首先得说明的是这并不是真正的3D,模型被导出为一系列的单个图片,例如一个3D户型图,以某个视角旋转360°,渲染出一系列连续的单个图片文件. 2.在Image.MouseMove事件中添加相应代码 ...

  7. Scala入门之函数进阶

    /** * 函数式编程进阶: * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: * 2, 函数更长用的方式是匿名函数,定义的时候只需要说明输入参数的类型和函数体即可,不需要 ...

  8. 多线程处理中Future的妙用

    java 中Future是一个未来对象,里面保存这线程处理结果,它像一个提货凭证,拿着它你可以随时去提取结果.在两种情况下,离开Future几乎很难办.一种情况是拆分订单,比如你的应用收到一个批量订单 ...

  9. 1020理解MySQL——索引与优化

    转自http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性 ...

  10. “Ceph浅析”系列之五——Ceph的工作原理及流程

    本文将对Ceph的工作原理和若干关键工作流程进行扼要介绍.如前所述,由于Ceph的功能实现本质上依托于RADOS,因而,此处的介绍事实上也是针对RADOS进行.对于上层的部分,特别是RADOS GW和 ...