[Usaco2015 dec]Max Flow 树上差分
[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
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
#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 树上差分的更多相关文章
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- BZOJ 4390: [Usaco2015 dec]Max Flow
4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 177 Solved: 113[Submi ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)
题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...
- 洛谷3128 [USACO15DEC]最大流Max Flow——树上差分
题目:https://www.luogu.org/problemnew/show/P3128 树上差分.用离线lca,邻接表存好方便. #include<iostream> #includ ...
- P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
- 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 ...
- 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分
题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...
- [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 204 Solved: 129[Submit][Status][Discuss] Descriptio ...
随机推荐
- C中 snprintf()函数的作用
函数原型:int snprintf(char* dest_str,size_t size,const char* format,...); 函数功能:先将可变参数 “…” 按照format的格式格式化 ...
- vue---day02
1. 全局组件的注册 - 创建根实例的时候,data可以是object,也可以是函数 - 创建组件的时候,data必须是函数 1.1 创建 Vue.component('global-componen ...
- react项目中引入百度地图打包报错问题
一.我正常引入百度地图,调试时候是好使的,但是打包时候就报错 引入方法如下: 报错如图 正常调试是好使的,但是打包报这个错,解析不了这个BMap,那么怎么办呢? 然后我就转用了window办法,虽然因 ...
- python第一个程序-->hello world
最近在网上看到一个小笑话,一个程序员的自我嘲讽:“我精通所以计算机语言的hello world!” 好了,废话不多说了,开始撸代码: 我本人用的是python3.6版本,各位可以通过官网下载自己喜欢的 ...
- 浅析 Linux 初始化 init 系统,Systemd
原文地址:http://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/ Systemd 的简介和特点 Systemd 是 Linux 系 ...
- nginx error_page
1. error_page语法 语法: error_page code [ code... ] [ = | =answer-code ] uri | @named_location 默认值: no 使 ...
- [转]struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor
package com.sise.action; import java.util.Map; import com.opensymphony.xwork2.Action; import com ...
- QSS 的选择器
本文连接地址:http://www.qtdebug.com/QSS-Selector.html 选择器决定了 style sheet 作用于哪些 Widget,QSS 支持 CSS2 定义的所有选择器 ...
- CSP201409-1:相邻数对
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- LeetCode 86 ——分隔链表
1. 题目 2. 解答 从前向后遍历链表,将结点值小于 x 的结点放入到新链表 1 中,将结点值大于等于 x 的结点放入新链表 2 中.最后,将新链表 2 拼接在新链表 1 后面即可. /** * D ...