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. Matlab 之 find()函数

    当我第一次用matlab语言编写一个工程项目时,发现自己编写的脚本里循环特别多,导致编程效率很低,这让我特别苦恼.有一次导师让我阅读他编写的一个Matlab脚本,并按照新要求对其进行更改.我发现脚本里 ...

  2. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

  3. React Native 之 Touchable 介绍与使用

    前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...

  4. Java 中的集合接口——List、Set、Map

    Java 中的集合接口——List.Set.Map 什么叫集合:集合就是Java API所提供的一系列类的实例,可以用于动态存放多个对象.这跟我们学过的数组差不多,那为什么我们还要学集合,我们看看数组 ...

  5. SQLSERVER 2012 收缩日志

    select log_reuse_wait_desc from sys.databases where name='tfs_CARDLANWEB' backup log tfs_CARDLANWEB ...

  6. Visual Studio 生成事件命令

    Visual Studio在生成项目工程前后,有时我们需要做一些特殊的操作,比如:拷贝生成的dll到指定目标下面等. 结合VS可以添加预先生成事件和后期生成事件,采用命令或bat批处理. 1.Visu ...

  7. Hbase入门教程--单节点伪分布式模式的安装与使用

    Hbase入门简介 HBase是一个分布式的.面向列的开源数据库,该技术来源于 FayChang 所撰写的Google论文"Bigtable:一个结构化数据的分布式存储系统".就像 ...

  8. Java 类的实例变量初始化的过程 静态块、非静态块、构造函数的加载顺序

    先看一道Java面试题: public class Baset { private String baseName = "base"; // 构造方法 public Baset() ...

  9. 理解SVG的viewport,viewBox,preserveAspectRatio

    万丈高楼平地起,基础很重要. viewport 表示SVG可见区域的大小,或者可以想象成舞台大小,画布大小. <svg width="500" height="30 ...

  10. JsonResult作为Action返回值时的错误

    JsonResult作为Action返回值时的错误   System.InvalidOperationException: This request has been blocked because ...