BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 177 Solved: 113
[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
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
HINT
Source
蒟蒻上来就想DFS序+线段树维护区间修改及查询最值,后来看到度娘上好多喊树链剖分的,又看到可以直接两边DFS+LCA切掉。
对于x到y的路径上的所有点+1,等同于对x做+1,对y做+1,对LCA(x,y)做-1,对LCA(x,y)->father做-1,最后让每个点的权值等于子树权值和即可。
#include <cstdio>
#include <cstring> const int siz = ; int n, m; int tot;
int hd[siz];
int to[siz];
int nt[siz]; inline void add(int x, int y)
{
nt[tot] = hd[x]; to[tot] = y; hd[x] = tot++;
nt[tot] = hd[y]; to[tot] = x; hd[y] = tot++;
} int dp[siz];
int fa[siz][]; void prework(int u, int f)
{
for (int i = ; i < ; ++i)
fa[u][i] = fa[fa[u][i - ]][i - ]; for (int i = hd[u]; ~i; i = nt[i])
if (to[i] != f)
{
int v = to[i];
dp[v] = dp[u] + ;
fa[v][] = u;
prework(v, u);
}
} inline int lca(int a, int b)
{
if (dp[a] < dp[b])
a ^= b ^= a ^= b; for (int i = ; i >= ; --i)
if (dp[fa[a][i]] >= dp[b])
a = fa[a][i]; if (a == b)return a; for (int i = ; i >= ; --i)
if (fa[a][i] != fa[b][i])
a = fa[a][i], b = fa[b][i]; return fa[a][];
} int sm[siz]; inline void solve(int x, int y)
{
int t = lca(x, y); ++sm[x];
++sm[y];
--sm[t];
--sm[fa[t][]];
} int ans; void calc(int u, int f)
{
for (int i = hd[u]; ~i; i = nt[i])
if (to[i] != f)
{
int v = to[i];
calc(v, u);
sm[u] += sm[v];
} if (ans < sm[u])
ans = sm[u];
} signed main(void)
{
scanf("%d%d", &n, &m); memset(hd, -, sizeof(hd)); for (int i = , x, y; i < n; ++i)
scanf("%d%d", &x, &y), add(x, y); dp[] = ; prework(, ); for (int i = , x, y; i <= m; ++i)
scanf("%d%d", &x, &y), solve(x, y); calc(, ); printf("%d\n", ans);
}
@Author: YouSiki
BZOJ 4390: [Usaco2015 dec]Max Flow的更多相关文章
- [Usaco2015 dec]Max Flow 树上差分
[Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 353 Solved: 236[Submit][Sta ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)
题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...
- 【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 ...
- [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 204 Solved: 129[Submit][Status][Discuss] Descriptio ...
- bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John ...
- 【刷题】BZOJ 4391 [Usaco2015 dec]High Card Low Card
Description Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of ...
- BZOJ 4393: [Usaco2015 Dec]Fruit Feast
DP #include<cstdio> using namespace std; int T,A,B,F[5000005],G[5000005]; int main(){ scanf(&q ...
- 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)
[BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...
随机推荐
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 13.中英文版本切换设计
目 录 第十三章 中英文版本切换设计... 2 13.1 不用自带的资源文件的理由... 2 13.2 配置文件... 2 13.3 语言 ...
- JAVA 问题集中之处以及解决的办法
也许当你看的时候,你可能认为这些都是简单的问题,有什么好记的.其实不是,我认为,我们往往是因为粗心而造成的错误,当你在开发中碰到这些问题时,你能一下看出来是什么错误,达到提高效率.而且往往你把小的问题 ...
- 16款最佳的 jQuery Time Picker 时间选择插件
jQuery 插件可以为你做许多事情,你可以很容易地把这些插件集成到您的网站.网络上的 jQuery 日期选择器和日历插件很多,但找不到很满意的时间选择器插件. 在这里,我们收集了最好的一组 jQue ...
- iOS之获取App的LaunchImage
作者:里脊串 授权本站转载. 启动图(LaunchImage)的管理其实在iOS开始中算比较简单的了,尤其是Xcode引入了xcassets之后,完全是傻瓜式的操作.但是有的时候我们还是需要在Laun ...
- Animation
Animation 效果 用法 1.非常简单,导入两个文件(UIView+SetRect) (UIView+ImageEffects) 源码 github源码:https://github.com/m ...
- Android Weekly Notes Issue #230
Android Weekly Notes Issue #230 November 6th, 2016 Android Weekly Issue #230. Android Weekly笔记, 本期内容 ...
- React Native 之 Touchable 介绍与使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...
- SQL基础教程--实现增删查改功能(W3School)
1.SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新 ...
- C#用链式方法表达循环嵌套
情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中单,上单,ADC,辅助:第二局新 ...
- “会”和 "好”纯粹是两个概念
你会吗? 如果我现在问下大家你会OOP 吗?你会OOD吗? 你知道SOLID吗?你会在实际工作中运用这些原则吗? 你知道模式吗,你会在实际项目中适时引入合理的设计模式来解决项目中的代码坏味吗? 你知道 ...