洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

时间:2021-11-22 13:57:05

题目描述

Farmer John has installed a new system of 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] pipes to transport milk between the 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] stalls in his barn (洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]), conveniently numbered 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

FJ is pumping milk between 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] pairs of stalls (洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]). For the 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]th such pair, you are told two stalls 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分], 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 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]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 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] to 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分], then it counts as being pumped through the endpoint stalls 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and

洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分], 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 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分].

The next 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] lines each contain two integers 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] (洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分]) describing a pipe

between stalls 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分].

The next 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] lines each contain two integers 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] and 洛谷P3128 [USACO15DEC]最大流Max Flow  [树链剖分] 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 &lbrack;USACO15DEC&rsqb;最大流Max Flow

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

  2. 洛谷 P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow

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

  3. 洛谷 P3128 &lbrack; USACO15DEC &rsqb; 最大流Max Flow —— 树上差分

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

  4. 洛谷P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow &lbrack;倍增LCA&rsqb;

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

  5. 洛谷P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow&lpar;树上差分&rpar;

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

  6. 洛谷——P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow

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

  7. 洛谷P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow (树上差分)

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

  8. 题解——洛谷P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow

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

  9. 洛谷 P3128 &lbrack;USACO15DEC&rsqb;最大流Max Flow-树上差分&lpar;点权&sol;点覆盖&rpar;&lpar;模板题&rpar;

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

随机推荐

  1. JS验证输入网址

    function CheckUrl(a) {    var falg = true;    var regstr = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- . ...

  2. jquery获取datagrid多选值

    var checkedItems = $('#dg').datagrid('getChecked'); $.each(checkedItems, function (index, item) { al ...

  3. 用一行代码初始化ArrayList

    方法1: ArrayList<String> arrList1 = (ArrayList<String>) Arrays.asList("Buenos Aires&q ...

  4. 关于NPC和NP-Hard问题

    参考链接: 1. P.NP.NPC和NP-hard问题的理解 参考:<算法导论>

  5. SFP光模块与SFP&plus;、XFP、QSFP、GBIC、BIDI的区别

    SFP.SFP+.XFP.QSFP.GBIC和BIDI等不同封装类型光模块不断推陈出新,我们就以市场上比较常见的为主,来谈谈它与其他类似光模块的区别. SFP光模块 SFP光模块又称⼩封装可插拔光模块 ...

  6. Windows上SQLPLUS的设置

    sqlplus启动的时候会调用login.sql,首先在当前路径下查找login.sql,如果没有找到,则在SQLPATH中查找该文件 另外sqlplus执行命令的时候也会首先在当前目录查找脚本,如果 ...

  7. java框架之SpringBoot&lpar;7&rpar;-异常处理

    前言 在 SpringBoot 项目中,默认情况下,使用浏览器访问一个不存在的地址会返回如下错误页面: 而当客户端未非浏览器时,错误信息则会以 json 数据返回,如下: 会出现如上效果的原因是 Sp ...

  8. day9-复习学习python实例

    学习实例代码 #求1到100的和print ("##################1到100求和#################")def sum(a,b): s = 0 fo ...

  9. mysql优化建议21条

    转自: http://blog.csdn.net/waferleo/article/details/7179009 今 天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于 ...

  10. nodejs中间件拦截,express不登录无法进入后台页面

    22.设置拦截 只有登录才能进入到后台页面,不登录无法进入 如果登陆成功, 写入session, 参数 uid uid=123dsfjksldfjsl 检测登陆, 请求中 session 是否包含 u ...