[luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]
题目描述
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
树上差分膜版题
浪费代码长度
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f inline int read(){
int re=;
char ch;
bool flag=;
while((ch=getchar())!='-'&&(ch<''||ch>''));
ch=='-'?flag=:re=ch-'';
while((ch=getchar())>=''&&ch<='') re=(re<<)+(re<<)+ch-'';
return flag?-re:re;
} struct edge{
int to,next;
edge(int to=,int next=):
to(to),next(next){}
}; struct ask{
int ss,tt,lca;
ask(int ss=,int tt=,int lca=):
ss(ss),tt(tt),lca(lca){}
}; const int maxn=; vector<edge> edges;
vector<edge> tree;
vector<edge> ques;
vector<ask> qu;
int n,q,cnt,root=,ans=-inf;
int head[maxn],tmp_head[maxn],had[maxn],fat[maxn];
int par[maxn],sum[maxn];
bool vis[maxn]; inline void add_edge(int from,int to){
edges.push_back(edge(to,head[from]));
head[from]=++cnt;
edges.push_back(edge(from,head[to]));
head[to]=++cnt;
} inline void add_tree(int from,int to){
tree.push_back(edge(to,tmp_head[from]));
tmp_head[from]=++cnt;
} void make_tree(int x,int fa){
fat[x]=fa;
for(int ee=head[x];ee;ee=edges[ee].next)
if(edges[ee].to!=fa){
add_tree(x,edges[ee].to);
make_tree(edges[ee].to,x);
}
} inline void add_ques(int ss,int tt){
ques.push_back(edge(tt,had[ss]));
had[ss]=++cnt;
ques.push_back(edge(ss,had[tt]));
had[tt]=++cnt;
} void init(){
n=read(); q=read();
edges.push_back(edge(,));
cnt=;
for(int i=;i<n;i++){
int from=read(),to=read();
add_edge(from,to);
} cnt=;
tree.push_back(edge(,));
make_tree(root,);
swap(head,tmp_head); cnt=;
ques.push_back(edge(,));
for(int i=;i<q;i++){
int ss=read(),tt=read();
qu.push_back(ask(ss,tt,));
add_ques(ss,tt);
}
} int find(int x){
return par[x]==x?x:par[x]=find(par[x]);
} void tarjan(int x){
for(int ee=head[x];ee;ee=tree[ee].next){
tarjan(tree[ee].to);
par[tree[ee].to]=x;
vis[tree[ee].to]=;
}
for(int ee=had[x];ee;ee=ques[ee].next)
if(vis[ques[ee].to])
qu[(ee-)>>].lca=find(ques[ee].to);
} void dfs_sum(int x){
for(int ee=head[x];ee;ee=tree[ee].next){
dfs_sum(tree[ee].to);
sum[x]+=sum[tree[ee].to];
}
} void solve(){
for(int i=;i<=n;i++) par[i]=i;
vis[root]=;
tarjan(root); for(int i=;i<q;i++){
ask qq=qu[i];
sum[qq.ss]++;
sum[qq.tt]++;
sum[qq.lca]--;
sum[fat[qq.lca]]--;
}
dfs_sum(root); for(int i=;i<=n;i++)
ans=max(ans,sum[i]);
printf("%d\n",ans);
} int main(){
//freopen("temp.in","r",stdin);
init();
solve();
return ;
}
Goodbye, my almost lover
再见了,我无缘的爱人
Goodbye, my hopeless dream
再见了,我无望的梦想
[luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]的更多相关文章
- bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)
题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...
- [Luogu 3128] USACO15DEC Max Flow
[Luogu 3128] USACO15DEC Max Flow 最近跟 LCA 干上了- 树剖好啊,我再也不想写倍增了. 以及似乎成功转成了空格选手 qwq. 对于每两个点 S and T,求一下 ...
- 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 ...
- 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 (树上差分)
跟LOJ10131暗的连锁 相似,只是对于\(lca\)节点把它和父亲减一 #include <cstdio> #include <iostream> #include < ...
- [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)
题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...
- P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of pipes to transport mil ...
- [LUOGU] P3128 [USACO15DEC]最大流Max Flow
题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...
随机推荐
- Coursera 机器学习笔记(四)
主要为第六周内容机器学习应用建议以及系统设计. 下一步做什么 当训练好一个模型,预测未知数据,发现结果不如人意,该如何提高呢? 1.获得更多的训练实例 2.尝试减少特征的数量 3.尝试获得更多的特征 ...
- python编写知乎爬虫实践
爬虫的基本流程 网络爬虫的基本工作流程如下: 首先选取一部分精心挑选的种子URL 将种子URL加入任务队列 从待抓取URL队列中取出待抓取的URL,解析DNS,并且得到主机的ip,并将URL对应的网页 ...
- javascript中event.clientX和event.clientY用法的注意事项
今天做项目用到了event.clientX和event.clientY,给元素定位,用定位的时候,让top和left等于事件元素的的坐标 <!DOCTYPE html> <html& ...
- 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】
平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...
- orcale设置自增列
create sequence SEQ_ERRORID minvalue 1 maxvalue 99999999 start with 1000 increment by 1 nocache orde ...
- Ext表格分页
pageSize:配置表格或者数据的数量, autoLoad: { start: 0, limit: 2 }:自动加载时候的参数, proxy中:params: {start: 0,limit: 2} ...
- go 接口案例,音乐播放器
manager.go //package main package mlib import "errors" type MusicEntry struct { Id string ...
- Java IO学习笔记四
内存操作流 之前的所有的流操作都是针对文件的,但是有时候只是想要实现数据间转换,此时如果我们想要创建一个文件然后再删除文件,那样显得有点麻烦,因此此时的内存操作流就显得很适合这类的操作,因为它只是在内 ...
- Jmeter元件运行顺序
JMeter执行顺序逻辑如下: 1.配置元件(如果存在) 2.前置处理器(如果存在) 3.定时器(如果存在) 4.取样器(如果存在) 5.后置处理器(如果存在且取样器的结果不为空) 6.断言(如果存在 ...
- JAVA项目省市县加载兼容浏览器异常
最近同僚遇到个在IE8及IE8以下浏览器上出现地址加载问题,初始加载没问题,问题出在事件上. 先来一段初始加载地址的代码:(也可以修改或者增加第二个事件地址) var select1 = new Li ...