题目描述

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. C#:DataTable映射成Model

    这是数据库开发中经常遇到的问题,当然,这可以用现成的ORM框架来解决,但有些时候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,还得自己处理. 反射自然必不可少的,另外考 ...

  2. lecture2-NN结构的主要类型的概述和感知机

    Hinton课程第二课 一.NN结构的主要类型的概述 这里的结构就是连接在一起的神经元.目前来说,在实际应用中最常见的NN就是前向NN,他是将数据传递给输入单元,通过隐藏层最后到输出层的单元:一个更有 ...

  3. 前端科普文—为什么<!DOCTYPE> 不可或缺

    When question comes 你一定在 HTML 页面最前面看到过这样一行代码(比如 百度): <!DOCTYPE html> 或者说类似这样的(比如 博客园-韩子迟 PS:博客 ...

  4. bootstrap - typeahead自动补全插件

    $('#Sale').typeahead({ ajax: { url: '@Url.Action("../Contract/GetSale")', //timeout: 300, ...

  5. EmitMapper的使用

    1.普通的映射. public class UserInfo { public int id { get; set; } public string name { get; set; } public ...

  6. 东大OJ 2SAT 异或

    看了十年才懂懂了十年才会会了十年才会写写了十年才写完写完了十年才能改对 #include<stdio.h> #include<string.h> struct res{ int ...

  7. 【JavaEE企业应用实战学习记录】struts2登录

    <%-- login.jsp Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 16:26 To chang ...

  8. iOS--更新cooped库

  9. 十天冲刺---Day2

    站立式会议 站立式会议内容总结: git上Issues新增内容: 燃尽图 照片

  10. Windows下Php安装mongodb扩展失败

    查看php版本 下载对应的mongodb插件 将php_mongo.dll文件复制到php安装目录下的ext下 重启apache Apache –k restart 浏览器php.info( )测试 ...