题目描述

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. CastleActiveRecord在多线程 事务提交时数据库资源竞争导致更新失败的测试结果记录

    CastleActiveRecord 经过测试,隔离级别: // 摘要: ,         ,         ,         ,         ,         ,         ,   ...

  2. QT QT练习一

    界面中通过三个 QLineEdit控件,一个QPushButton实现+ - * /四则运算,点击pushbutton后将运算结果显示在QLabel控件上. #ifndef WIDGET_H #def ...

  3. NOI2018准备Day5

    3个半小时,连看题解带超过了一道二分题.

  4. checkbox页面全选

    http://pan.baidu.com/s/1tfzSa

  5. QDir的mkdir和mkpath区别

    mkdir:上层目录不存在时,创建会失败.比如创建“c:\\test\test”,如果test不存在,那test也创建不了.目录已经存在时会返回false. mkpath:上层目录不存在也没关系,自动 ...

  6. Spring 依赖注入方式详解

    平常的Java开发中,程序员在某个类中需要依赖其它类的方法. 通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理. Spring提出了依赖注入的思想,即依赖类不由 ...

  7. IntelliJ IDEA,代码行宽度超出限制时自动换行

    转自:http://my.oschina.net/angerbaby/blog/471351 当我们使用IDE写代码时,为了保证代码的可阅读性和优雅性,通常会借助IDE的代码风格设置功能,令IDE智能 ...

  8. Orchard创建全局应用

    Orchard的本地化管理托管于一个外部服务(Crowdin),这个项目是公开的且欢迎大家做贡献. Orchard支持两种类型的本地: Orchard应用程序以及已安装模块中的文本字符串的本地化(其实 ...

  9. GitHub 上一份很受欢迎的前端代码优化指南-强烈推荐收藏

    看到一份很受欢迎的前端代码指南,根据自己的理解进行了翻译,但能力有限,对一些JS代码理解不了,如有错误,望斧正. HTML 语义化标签 HTML5 提供了很多语义化元素,更好地帮助描述内容.希望你能从 ...

  10. B树和B+树

    当数据量大时,我们如果用二叉树来存储的会导致树的高度太高,从而造成磁盘IO过于频繁,进而导致查询效率下降.因此采用B树来解决大数据存储的问题,很多数据库中都是采用B树或者B+树来进行存储的.其目的就是 ...