题目描述

Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between  pairs of stalls (). For the th such pair, you are told two stalls  and , 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 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  to , then it counts as being pumped through the endpoint stalls  and

, as well as through every stall along the path between them.

FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N。所有隔间都被管道连通了。

FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少。

输入输出格式

输入格式:

The first line of the input contains  and .

The next  lines each contain two integers  and  () describing a pipe

between stalls  and .

The next  lines each contain two integers  and  describing the endpoint

stalls of a path through which milk is being pumped.

输出格式:

An integer specifying the maximum amount of milk pumped through any stall in the

barn.

输入输出样例

输入样例#1:

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
输出样例#1:

9

虽然名叫Max Flow,但是和网络流半点关系没有。

正解似乎是利用树剖求LCA,然后按照差分权值。(树上差分,结点权值等于其子树的差分累计结果)

然而并没有多想就写了树剖+线段树暴力维护链修改和区间最大值……

1825ms龟速水过。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k;
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
struct node{
int f,son;
int top,size,dep;
int w,e;
}tr[mxn];
struct segtree{
int mk,mx;
}st[mxn<<];
int sz=;
void DFS1(int u){
tr[u].size=;
tr[u].son=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==tr[u].f)continue;
tr[v].f=u;
tr[v].dep=tr[u].dep+;
DFS1(v);
tr[u].size+=tr[v].size;
if(tr[v].size>tr[tr[u].son].size)tr[u].son=v;
}
return;
}
void DFS2(int u,int top){
tr[u].top=top;
tr[u].w=++sz;
if(tr[u].son){
DFS2(tr[u].son,top);
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v!=tr[u].f && v!=tr[u].son)
DFS2(v,v);
}
}
tr[u].e=sz;
return;
}
void pushdown(int l,int r,int rt){
if(l==r)return;
int mid=(l+r)>>;
st[rt<<].mk+=st[rt].mk;
st[rt<<|].mk+=st[rt].mk;
st[rt<<].mx+=st[rt].mk;
st[rt<<|].mx+=st[rt].mk;
st[rt].mk=;
return;
}
void add(int L,int R,int v,int l,int r,int rt){
if(L<=l && r<=R){
st[rt].mk+=v;
st[rt].mx+=v;
return;
}
if(st[rt].mk)pushdown(l,r,rt);
int mid=(l+r)>>;
if(L<=mid)add(L,R,v,l,mid,rt<<);
if(R>mid)add(L,R,v,mid+,r,rt<<|);
st[rt].mx=max(st[rt<<].mx,st[rt<<|].mx);
return;
}
int qmx(int L,int R,int l,int r,int rt){
if(L<=l && r<=R)return st[rt].mx;
if(st[rt].mk)pushdown(l,r,rt);
int mid=(l+r)/;
int res=-1e9;
if(L<=mid)res=max(res,qmx(L,R,l,mid,rt<<));
if(R>mid)res=max(res,qmx(L,R,mid+,r,rt<<|));
return res;
}
void qadd(int x,int y){
while(tr[x].top!=tr[y].top){
if(tr[tr[x].top].dep<tr[tr[y].top].dep)swap(x,y);
add(tr[tr[x].top].w,tr[x].w,,,n,);
x=tr[tr[x].top].f;
}
if(tr[x].w>tr[y].w)swap(x,y);
add(tr[x].w,tr[y].w,,,n,);
return;
}
int main(){
n=read();k=read();
int i,j,u,v,x,y;
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);
add_edge(v,u);
}
DFS1();
DFS2(,);
for(i=;i<=k;i++){
/*
for(i=1;i<=sz;i++){
printf("%d ",qmx(tr[i].w,tr[i].w,1,n,1));
}
printf("\n");*/
x=read();y=read();
qadd(x,y);
}
printf("%d\n",st[].mx);
return ;
}

洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]的更多相关文章

  1. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  2. 洛谷 P3128 [USACO15DEC]最大流Max Flow

    题目描述 \(FJ\)给他的牛棚的\(N(2≤N≤50,000)\)个隔间之间安装了\(N-1\)根管道,隔间编号从\(1\)到\(N\).所有隔间都被管道连通了. \(FJ\)有\(K(1≤K≤10 ...

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

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

  4. 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

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

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

  6. 洛谷——P3128 [USACO15DEC]最大流Max Flow

    https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to ...

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

    ###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a  b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...

  8. 题解——洛谷P3128 [USACO15DEC]最大流Max Flow

    裸的树上差分 因为要求点权所以在点上差分即可 #include <cstdio> #include <algorithm> #include <cstring> u ...

  9. 洛谷 P3128 [USACO15DEC]最大流Max Flow-树上差分(点权/点覆盖)(模板题)

    因为徐州现场赛的G是树上差分+组合数学,但是比赛的时候没有写出来(自闭),背锅. 会差分数组但是不会树上差分,然后就学了一下. 看了一些东西之后,对树上差分写一点个人的理解: 首先要知道在树上,两点之 ...

随机推荐

  1. How To Create an IE-Only Stylesheet

    https://css-tricks.com/how-to-create-an-ie-only-stylesheet/ https://css-tricks.com/snippets/css/css- ...

  2. 毫米转换为PX

    公式:毫米数/25.4*你的电脑的DPI,win7 DPI  100%缩放为96,125%为120,150%为144,200%为192 象素数 / DPI = 英寸数 英寸数 * 25.4 = 毫米数

  3. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  4. 数据库MySQL与Oracle的一些去O注意项

    一.oracle递归查询语句start with ...connect by prior ① 给你一张表,表里面有主键id,以及该项的父节点parent_id,查询出该表中所有的父子关系节点树? Or ...

  5. Http概述(一)

    Http使用的是可靠的数据传输协议,因此即使数据来自地球的另一端,也能够确保数据在传输过程中不会被损坏或产生混乱. 这样用户在访问信息时就不用担心其完整性了. web服务端与服务器是如何通信的 Web ...

  6. Windows下Memcache的安装及PHP扩展配置

    一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcached.exe 二.安装 WIN7 64位双击打开这个exe可能只有一个空的窗口,不能输入任何命令, ...

  7. Java学习笔记(二二)——Java HashMap

    [前面的话] 早上起来好瞌睡哈,最近要注意一样作息状态.       HashMap好好学习一下. [定义] Hashmap:是一个散列表,它存储的内容是键值对(key——value)映射.允许nul ...

  8. Bootstrap系列 -- 9. 表格

    一. Bootstrap 表格样式支持 Bootstrap提供了六种不同风格的样式支持,其中一个基础样式,4个附件样式,1个响应式设计样式 1. .table:基础表格 2. .table-strip ...

  9. SQL Server 2012新特性(1)T-SQL操作FileTable目录实例

    在SQL Server 2008提供FileStream,以借助Windows系统本身的API来强化SQL Server对于非结构化数据的支持后,SQL Server 2012更是推出了像Contai ...

  10. IL指令大全(转)

    名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...