[Usaco2015 dec]Max Flow

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

 
数上差分,对于x,y 在x处+1,y处+1,lca(x,y)-1 fa[lca(x,y)]-1,就可以了。
 #include<cstring>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<algorithm> #define N 50007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,ans;
int val[N],dep[N],fa[N][];
int cnt,hed[N],nxt[N<<],rea[N<<]; void add(int u,int v)
{
nxt[++cnt]=hed[u];
hed[u]=cnt;
rea[cnt]=v;
}
void add_two_way(int x,int y)
{
add(x,y);
add(y,x);
}
void dfs_init(int u,int par)
{
for (int i=;(<<i)<=dep[u];i++)
fa[u][i]=fa[fa[u][i-]][i-];
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i];
if (v==par) continue;
fa[v][]=u;dep[v]=dep[u]+;
dfs_init(v,u);
}
}
int lca(int a,int b)
{
if (dep[a]<dep[b]) swap(a,b);
int i;for (i=;(<<i)<=dep[a];i++);i--;
for (int j=i;j>=;j--)
if (dep[a]-(<<j)>=dep[b]) a=fa[a][j];
if (a==b) return a;
for (int j=i;j>=;j--)
if (fa[a][j]!=fa[b][j]) a=fa[a][j],b=fa[b][j];
return fa[a][];
}
void solve(int u)
{
for (int i=hed[u];i!=-;i=nxt[i])
{
int v=rea[i];
if (v==fa[u][]) continue;
solve(v),val[u]+=val[v];
}
ans=max(val[u],ans);
}
int main()
{
memset(hed,-,sizeof(hed));
n=read(),m=read();
for (int i=;i<n;i++)
add_two_way(read(),read());
dfs_init(,);
for (int i=;i<=m;i++)
{
int x=read(),y=read();
val[x]++,val[y]++,val[lca(x,y)]--,val[fa[lca(x,y)][]]--;
} solve();
printf("%d\n",ans);
}

[Usaco2015 dec]Max Flow 树上差分的更多相关文章

  1. BZOJ4390: [Usaco2015 dec]Max Flow

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

  2. BZOJ 4390: [Usaco2015 dec]Max Flow

    4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 177  Solved: 113[Submi ...

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

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

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

    题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...

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

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

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

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

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

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

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

  9. [Usaco2015 dec]Max Flow

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

随机推荐

  1. 嵌入式框架Zorb Framework搭建六:定时器的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  2. Linux mysql启动与关闭

    service mysql stop service mysqld start

  3. ccf201703-2 STLlist

    题目:http://118.190.20.162/view.page?gpid=T56 问题描述 体育老师小明要将自己班上的学生按顺序排队.他首先让学生按学号从小到大的顺序排成一排,学号小的排在前面, ...

  4. DESCRIBEFIELD

    実行時データ型識別.略語は RTTI です.プログラム実行時にデータ型を識別して処理を行う仕組みです.. DESCRIBE FIELD命令を使用 DESCRIBE FIELD命令を使用して.変数のデー ...

  5. 【SAPUI5】ODataを構成するもの

    はじめに SAPUI5でアプリケーションを作るにあたり.ODataは避けては通れないトピックです.結構広いテーマなので.5-7回くらいに分けて書きたいと思います.1回目はODataの概要について説明し ...

  6. Android面试收集录 数据库

    1.SQLite数据库如何查询表table1的第20条到30条记录? select * from table1 limit 19,11   ==>从19开始,11个数据 2.如何才能将table ...

  7. CCS实例,网页栏目

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. 【C#】 引用类型

    [C#] 引用类型 附图和代码为了便于理解引用类型 public static void RefDemo() { RefClass r1 = new RefClass { Name = "r ...

  9. c++ class as sort function

    // constructing sets #include <iostream> #include <set> #include <string.h> bool f ...

  10. Viewer.js 图片预览插件使用

    一.简介 Viewer.js 是一款强大的图片查看器. Viewer.js 有以下特点: 支持移动设备触摸事件 支持响应式 支持放大/缩小 支持旋转(类似微博的图片旋转) 支持水平/垂直翻转 支持图片 ...