P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
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.
输入输出样例
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
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+树上差分)的更多相关文章
- luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
- luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)
链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...
- [USACO15DEC]最大流Max Flow(树上差分)
题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN st ...
- LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)
跟LOJ10131暗的连锁 相似,只是对于\(lca\)节点把它和父亲减一 #include <cstdio> #include <iostream> #include < ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow(树上差分)
题意 题目链接 Sol 树上差分模板题 发现自己傻傻的分不清边差分和点差分 边差分就是对边进行操作,我们在\(u, v\)除加上\(val\),同时在\(lca\)处减去\(2 * val\) 点差分 ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow (树上差分)
###题目链接### 题目大意: 给你一棵树,k 次操作,每次操作中有 a b 两点,这两点路上的所有点都被标记一次.问你 k 次操作之后,整棵树上的点中被标记的最大次数是多少. 分析: 1.由于数 ...
随机推荐
- 「 Luogu P1231 」 教辅的组成
题目大意 有 $\text{N1}$ 本书 $\text{N2}$本练习册 $\text{N3}$本答案,一本书只能和一本练习册和一本答案配对.给你一些书和练习册,书和答案的可能的配对关系.问你最多可 ...
- HDU - 2044 - 一只小蜜蜂...(dp)
题意: 如题 思路: 仔细观察图 1-4和3-6其实是一样的答案,那么所有的方案都可以相减,意思为全部转化为从1开始 剩下的就是观察规律,仔细观察5号,能到5号蜂房的只有3和4,3和4到5号蜂房只有一 ...
- 使用scrapy 爬取酷狗音乐歌手及歌曲名并存入mongodb中
备注还没来得及写,共爬取八千多的歌手,每名歌手平均三十首歌曲算,大概二十多万首歌曲 run.py #!/usr/bin/env python # -*- coding: utf-8 -*- __aut ...
- C++ 输入外挂
inline int read() { int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch> ...
- node.js 利用流实现读写同步,边读边写
//10个数 10个字节,每次读4b,写1b let fs=require("fs"); function pipe(source,target) { //先创建可读流,再创建可写 ...
- hdu——3836 Equivalent Sets
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 【sql技巧】mysql修改时,动态指定要修改的字段 update `table` set (case when ....) = 1 where id = xx
如果你点进了这篇帖子,那么你一定遇到了跟我一样的问题.别看题目的set case when...,我一开始也是第一反应是用case when但是发现并不好使. 问题呢,说得高大上一点:动态指定要修改的 ...
- 如何使用IVT BlueSoleil 如何在电脑上使用蓝牙耳机
1 确保电脑上有蓝牙适配器 (现在很多电脑是不配蓝牙的),如果没有,网上买个蓝牙适配去,十几块钱很便宜.好了之后装一个下面这个软件,然后搜索蓝牙耳机,下面的状态栏就是"搜索设备" ...
- 查看程序占用tomcat内存情况
近期,公司线上tomcat常常无缘无辜宕机.总结了一下定位问题的方法,仅供參考: 报错信息: Maximum number of threads (200) created for connector ...
- Wireshark 抓包遇到 you don’t have permission to capture on that device mac 错误的解决方案
Wireshark 抓包遇到 you don’t have permission to capture on that device mac 错误的解决方案 上次有篇博客讲了如何利用wireshark ...