P3128 [USACO15DEC]最大流Max Flow

题目描述

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
/*
树上差分:对于树上x,y之间的路径区间修改时,设数组为c
则c[x]+1,c[y]+1,c[lca(x,y)]-1,c[father[lca(x,y)]-1.
最后dfs一下,使每个节点c[x]+=每个子节点的c值就ok了..
那就来说明一下树上差分这个据说完爆树剖的东西:
对于两个点,把他们的路径上所有点包括他们权值加一。开始路径上权值都为零,
先把起点终点权值加一,然后把它们分别往LCA权值上传,易知,LCA被加了2遍,所以减一。
因为标记上传时不可避免的把LCA的+1标记也上传了,所以就要把她减一成为零,然后上传。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100001
#define S 21 using namespace std;
int deep[maxn],head[maxn],p1,p2,n,m,num,ans,s,x,y,fa[maxn][S+];
int w[maxn],w2[maxn];
struct node {
int from;
int to;
int next;
}e[maxn*]; void add(int from,int to)
{
e[num].from=from;
e[num].to=to;
e[num].next=head[from];
head[from]=num;
num++;
} int init()
{
int x=,f=;char c=getchar();
while(c>''||c<''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} void swap(int &a,int &b)
{
int t=a;a=b;b=t;
} void get_fa()
{
for(int j=;j<=S;j++)
for(int i=;i<=n;i++)
fa[i][j]=fa[fa[i][j-]][j-];
} void Dfs(int now,int from,int c)
{
fa[now][]=from;
deep[now]=c;
for(int i=head[now];~i;i=e[i].next)
{
int& v=e[i].to;
if(v!=from)
Dfs(v,now,c+);
}
} int get_same(int a,int t)
{
for(int i=;i<S;i++)
if(t&(<<i)) a=fa[a][i];
return a;
} int LCA(int a,int b)
{
if(deep[a]<deep[b]) swap(a,b);
a=get_same(a,deep[a]-deep[b]);
if(a==b) return a;
for(int i=S;i>=;i--) {
if(fa[a][i]!=fa[b][i])
{
a=fa[a][i];
b=fa[b][i];
}
}
return fa[a][];
} void work(int u,int v)//树上差分
{
int s=LCA(u,v);
w[u]++;
w[v]++;
w[s]--;
if(fa[s][]!=-) w[fa[s][]]--;
} int dfs2(int now,int from)
{
w2[now]=w[now];
for(int i=head[now];~i;i=e[i].next)
{
int& v=e[i].to;
if(v!=from)
dfs2(v,now),
w2[now]+=w2[v];//上传标记
}
ans=max(ans,w2[now]);
return ans;
} int main()
{
memset(head,-,sizeof head);
n=init();m=init();
int x,y;
for(int i=;i<n;i++)
{
x=init();y=init();
add(x,y);
add(y,x);
}
Dfs(,-,);
get_fa();
for(int i=;i<=m;i++)
{
x=init();y=init();
work(x,y);
}
ans=dfs2(,-);
printf("%d\n",ans);
return ;
}

P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)的更多相关文章

  1. luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

  2. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

  3. [USACO15DEC]最大流Max Flow(树上差分)

    题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN st ...

  4. LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)

    跟LOJ10131暗的连锁 相似,只是对于\(lca\)节点把它和父亲减一 #include <cstdio> #include <iostream> #include < ...

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

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

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

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

  7. [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]

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

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

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

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

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

随机推荐

  1. JavaScript 面向对象的编程(二) 类的封装

    类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...

  2. mysql数据库变更监控(canal)

    背景: 1. 一些项目的基础功能会有Audit Trace, 以记录系统用户所做过的所有记录. 2. 实时备份数据,比如mysql主从复制,一个用于面向应用,一个用于对应用数据库的实时备份. 3. 实 ...

  3. Discuz论坛迁移需要修改的3个配置文件

    需要修改的3个地方: \config\config_global.php \config\config_ucenter.php \uc_server\data\config.inc.php

  4. 【Codeforces 382C】Arithmetic Progression

    [链接] 我是链接,点我呀:) [题意] 让你在n个数字中再加入一个数字 使得这n+1个数字排序之后 相邻两个数字的差都相同 [题解] 注意相邻为0的情况 这种情况 只有全都相同才行 只有一种情况 然 ...

  5. JavaSE 学习笔记之Jdk5.0新特性(十九)

    Jdk5.0新特性: Collection在jdk1.5以后,有了一个父接口Iterable,这个接口的出现的将iterator方法进行抽取,提高了扩展性. --------------------- ...

  6. HDU 4598 Difference

    Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  7. 九度oj 题目1045:百鸡问题

    题目1045:百鸡问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10418 解决:4559 题目描述: 用小于等于n元去买100只鸡,大鸡5元/只,小鸡3元/只,还有1/3元每只的一 ...

  8. Uva10562

    Professor Homer has been reported missing. We suspect that his recent research works might have had ...

  9. Bugzilla 系统企业应用案例

    目录 一. 概述: - 4 - 二. 目的 - 4 - 三. 执行原则 - 4 - 四. 管理办法 - 4 - 五. BUG处理流程图 - 5 - 六. 主要职责 - 6 - 七. 需求类问题处理 - ...

  10. Ajax提交post请求返回404错误

    最近使用ajax提交表单的时候,发现无法执行success函数,后台的代码也正常执行了,但是就是无法执行success函数,执行error函数,返回的错误代码时404.显然是找不到请求的url. 可是 ...