[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. 使用localStorage,sessionStorage,cookie等存储

    Web 存储 API 提供了 sessionStorage (会话存储) 和 localStorage(本地存储)两个存储对象来对网页的数据进行添加.删除.修改.查询操作. 特点: localStor ...

  2. PHP.46-TP框架商城应用实例-后台21-权限管理-权限和角色的关系

    权限和角色的关系 权限功能 角色功能 权限与角色的关联要通过权限-角色表进行{多对多} /********* 角色-权限表 *********/ drop if exists p39_role_pri ...

  3. struts2官方 中文教程 系列十:Form标签

    介绍 在本教程中,我们将探索其他Struts 2表单控件.在前面的教程中,我们介绍了如何使用Struts 2表单(处理表单.表单验证和消息资源文件),我们介绍了如何使用Struts 2 head, f ...

  4. 【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全

    PHP取整数函数常用的四种方法,下面收集了四个函数:经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已--主要是:ceil,floor,round,intval PHP取整数函数常用 ...

  5. jmeter常用的内置变量

    1. vars   API:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html vars.get(& ...

  6. react children技巧总结

    在使用该技巧时,建议先看一下相关的知识,点我查看 假如使用该属性时,想把父组件的所有属性及部分方法传递给子组件,该怎么办呢?看代码 const Child = ({ doSomething, valu ...

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

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

  8. 在Kotlin上怎样用Mockito2 mock final 类(KAD 23)

    作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/mockito-2-kotlin/ 如我们在前面文章中谈到的,Kotlin最 ...

  9. Java进阶知识点1:白捡的扩展性 - 枚举值也是对象

    一.背景 枚举经常被大家用来储存一组有限个数的候选常量.比如下面定义了一组常见数据库类型: public enum DatabaseType { MYSQL, ORACLE, SQLSERVER } ...

  10. POJ 2761 Feed the dogs(平衡树or划分树or主席树)

    Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...