4390: [Usaco2015 dec]Max Flow

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 156  Solved: 100
[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

HINT

Source

Platinum鸣谢Claris提供译文

[Submit][Status][Discuss]

【题解】【树上差分】

【在树中将所有路径起、始权值加1,起、始点的lca权值减2,从所有叶节点开始把权值往上累加。最终权值为路径数的点到其父亲的边为所求边。】

[写树链剖分T掉了。。。然后才知道还有这么一种神奇的方式]

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[100010],nxt[100010],p[50010],tot;
int f[50010][20],father[50010],dep[50010],size[50010],sum[50010];
int mi[20],n,k,ans;
inline void add(int x,int y)
{tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot;tot++; a[tot]=x; nxt[tot]=p[y]; p[y]=tot;
}
void dfs(int x,int fa,int h)
{dep[x]=h;  father[x]=fa;for(int i=1;i<16;++i) f[x][i]=f[f[x][i-1]][i-1];for(int i=p[x];i!=-1;i=nxt[i])if(a[i]!=fa){f[a[i]][0]=x;dfs(a[i],x,h+1);}
}
inline int lca(int x,int y)
{if(dep[x]<dep[y]) swap(x,y);for(int i=15;i>=0;--i)while(dep[f[x][i]]>=dep[y]) x=f[x][i];if(x==y) return x;for(int i=15;i>=0;--i)if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];return f[x][0];
}
inline void solve(int x,int fa)
{size[x]=sum[x];for(int i=p[x];i!=-1;i=nxt[i])if(a[i]!=fa){solve(a[i],x);size[x]+=size[a[i]];}ans=max(ans,size[x]);
}
int main()
{//freopen("int.txt","r",stdin);//freopen("my.txt","w",stdout); int i,j; memset(p,-1,sizeof(p));memset(nxt,-1,sizeof(nxt));scanf("%d%d",&n,&k);for(i=1;i<n;++i) {int x,y;scanf("%d%d",&x,&y);add(x,y);}dfs(1,0,1);for(i=1;i<=k;++i){int x,y;scanf("%d%d",&x,&y);int l=lca(x,y);sum[x]++; sum[y]++; sum[l]--; if(l!=1) sum[father[l]]--;}solve(1,0);printf("%d",ans);return 0;
}

转载于:https://www.cnblogs.com/lris-searching/p/9403037.html

【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)相关推荐

  1. bzoj4390[Usaco2015 dec]Max Flow*

    bzoj4390[Usaco2015 dec]Max Flow 题意: 给定一棵有N个点的树,所有节点的权值都为0.有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一.请输出K次操作完 ...

  2. BZOJ 4326 NOIP2015 运输计划(树上差分+LCA+二分答案)

    4326: NOIP2015 运输计划 Time Limit: 30 Sec  Memory Limit: 128 MB Submit: 1388  Solved: 860 [Submit][Stat ...

  3. bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...

  4. P3128-最大流Max Flow【树上差分,LCA】

    正题 题目大意 一棵树 若干条路径,哪个点经过的路径最多,求路径条数. 解题思路 对于每条路径计算一次LCALCALCA,然后树上差分就好了. codecodecode #include<cst ...

  5. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

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

  6. P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 对,这是一道最大流的题目qwq 树上跑最大流,没错 也就是跑最小割 你看名字里都有最大流,为什么不能跑最大流qwq............. ...

  7. P3128 [USACO15DEC]Max Flow P

    P3128 [USACO15DEC]Max Flow P 树上差分之点差分模板题 题目描述: FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道 ...

  8. [LUOGU] P3128 [USACO15DEC]最大流Max Flow

    题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...

  9. [NOIP 2015]运输计划-[树上差分+二分答案]-解题报告

    [NOIP 2015]运输计划 题面: A[NOIP2015 Day2]运输计划 时间限制 : 20000 MS 空间限制 : 262144 KB 问题描述 公元 2044 年,人类进入了宇宙纪元. ...

最新文章

  1. 20秒搭建web服务器,跨平台(mac,window)
  2. 第二次作业——个人项目实战
  3. 2015 UESTC Training for Search Algorithm String - M - Palindromic String【Manacher回文串】
  4. NSArray打印汉字的方法
  5. .NET 泛型,详细介绍
  6. 支持M1芯片mac 达芬奇17中文版(详细安装教程) DaVinci Resolve Studio 17.3.1b5
  7. MySQL启用SSL连接
  8. poj 1182 食物链 (并查集)
  9. 实分析royden第四版答案_高价实木变板木掺了“假”的实木家具!搞懂这些名词,买家具不上当...
  10. Matlab 小波变换dwt和wavedec
  11. 解决Scrapy抓取中文网页保存为json文件时中文不显示而是显示unicode的问题
  12. C++ 对象创建方式
  13. php中conf,php 中 php-fpm.conf
  14. 视频处理中各个分辨率/数字电视系统显示格式 的介绍(QCIF,CIF,4CIF,D1,720P,1080I,1080P等)
  15. Tkinter 组件详解(十三):Menu
  16. 腾讯云--OOS对象存储服务--java程序封装
  17. React移动端端局域网手机访问测试
  18. html5游戏 分数排行榜,热推游戏榜 9月份“微信HTML5游戏排行榜”
  19. Photoshop 入门教程「2」了解 Photoshop 工作区
  20. C#如何在list中添加序号

热门文章

  1. 正则表达式搜魂者(转:清清月儿 )
  2. showcase basketball stadium
  3. Flutter Image 图片加载
  4. Flutter中文本输入框TexeFieldr键盘样式TextInputType总结TexeField设置不可编辑
  5. Vue填坑(v-model和:model)
  6. 记一个 DataBindings遇到的坑,当ComboBox同时绑定DataSource与DataBindings的时候,绑定的元素一定要同名...
  7. Linux CentOS如何汉化系统
  8. 大学生创新项目认真投一篇中文核心的坎坷故事
  9. 0320 关于构建之法前三章的读后感
  10. 高质量程序设计指南c++/c语言(25)--类与内联函数