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

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

[Submit][Status][Discuss]

蒟蒻上来就想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的更多相关文章

  1. [Usaco2015 dec]Max Flow 树上差分

    [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 353  Solved: 236[Submit][Sta ...

  2. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

  3. bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)

    题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...

  4. 【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 ...

  5. [Usaco2015 dec]Max Flow

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 129[Submit][Status][Discuss] Descriptio ...

  6. bzoj 4397: [Usaco2015 dec]Breed Counting -- 前缀和

    4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John ...

  7. 【刷题】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 ...

  8. BZOJ 4393: [Usaco2015 Dec]Fruit Feast

    DP #include<cstdio> using namespace std; int T,A,B,F[5000005],G[5000005]; int main(){ scanf(&q ...

  9. 【BZOJ4391】[Usaco2015 dec]High Card Low Card(贪心)

    [BZOJ4391][Usaco2015 dec]High Card Low Card(贪心) 题面 BZOJ 题解 预处理前缀后缀的结果,中间找个地方合并就好了. #include<iostr ...

随机推荐

  1. 高性能 TCP & UDP 通信框架 HP-Socket v3.4.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  2. Java设计模式 -- 基本原则

    这两个星期开始系统地学习设计模式相关的知识,对每一个原则或者设计模式主要从下面几点分析学习: 定义:简单地描述其作用 解决问题:说明该原则或设计模式解决什么限制条件下的问题. 结构图:绘制相关例子的U ...

  3. 4.5 .net core下直接执行SQL语句并生成DataTable

    .net core可以执行SQL语句,但是只能生成强类型的返回结果.例如var blogs = context.Blogs.FromSql("SELECT * FROM dbo.Blogs& ...

  4. SQL Server导入数据时“启用标示插入”详解

    在SQL Server中导入数据时,会有一个"启用标示插入"的选项,突然间懵逼了,这到底啥意思?我选与不选这个选项,结果好像没区别!不科学啊这,"存在即合理", ...

  5. Android中使用AsyncTask实现文件下载以及进度更新提示

    Android提供了一个工具类:AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单.相对Handler来说AsyncTask更轻量级一些,适用于简单的异步处理,不需要借助线程和 ...

  6. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  7. emmet 系列(1)基础语法

    emmet 系列(1)基础语法 emmet 是一个能显著提升开发html和css开发效率的web开发者工具 emmet基本上目前已知的编辑器都有相应的插件,各个编辑器的emmet插件的下载地址:点我下 ...

  8. pip安装指定版本的package

    起因 最近到一个项目组,用了一套高大上的运维工具来搭建开发环境. 有vagrant控制VirtualBox启动虚拟机.有ansible来运行playbook初始化环境. 然后遇到了一个坑,项目现有的p ...

  9. 使用bulkload向hbase中批量写入数据

    1.数据样式 写入之前,需要整理以下数据的格式,之后将数据保存到hdfs中,本例使用的样式如下(用tab分开): row1 N row2 M row3 B row4 V row5 N row6 M r ...

  10. [转载]跨域iframe高度自适应

    场景: 经常会有这样的需求,跟外部合作伙伴合作,要嵌入别人的页面,这时候就需要高度自适应了,在这种跨域的情况下如何解决呢? 解决: 在iframe(合作伙伴的页面,称为P页面)中创建一个隐藏的ifra ...