bzoj 4390: [Usaco2015 dec]Max Flow(树链剖分+手写栈)
来源:互联网 发布:招商信诺 医疗网络部 编辑:程序博客网 时间:2024/06/01 21:41
4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 115 Solved: 69
[Submit][Status][Discuss]
Description
Farmer John has installed a new system of N−1 pipes to transport milk between the N stalls in his barn (2≤N≤50,000), conveniently numbered 1…N. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.
FJ is pumping milk between KK pairs of stalls (1≤K≤100,000). For the iith such pair, you are told two stalls sisi and titi, 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 KK 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 sisi to titi, then it counts as being pumped through the endpoint stalls sisi and titi, as well as through every stall along the path between them.
给定一棵有N个点的树,所有节点的权值都为0。
有K次操作,每次指定两个点s,t,将s到t路径上所有点的权值都加一。
请输出K次操作完毕后权值最大的那个点的权值。
Input
The first line of the input contains NN and KK.
The next N−1 lines each contain two integers x and y (x≠y,x≠y) describing a pipe between stalls x and y.
The next K lines each contain two integers ss and t describing the endpoint stalls of a path through which milk is being pumped.
Output
An integer specifying the maximum amount of milk pumped through any stall in the barn.
Sample Input
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
Sample Output
HINT
Source
Platinum鸣谢Claris提供译文
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 100003using namespace std;int n,m,sz,top,tot,st[N],use[N],cur[N],fa[N],belong[N];int son[N],point[N],deep[N],size[N],pos[N],a[N],val[N];int next[N*2],v[N*2];int tr[N*4],delta[N*4];void add(int x,int y){tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x;}void dfs(){top=0;for (int i=1;i<=n;i++) cur[i]=point[i];size[1]=1; deep[1]=1; st[++top]=1;while (top){int x=st[top];if (v[cur[x]]==fa[x]) cur[x]=next[cur[x]];if (!cur[x]){--top;if (fa[x]){size[fa[x]]+=size[x];if (size[son[fa[x]]]<size[x]) son[fa[x]]=x;}continue;}int vt=v[cur[x]];size[vt]=1; deep[vt]=deep[x]+1; fa[vt]=x;st[++top]=vt; cur[x]=next[cur[x]];}}void dfs2(){top=0; for (int i=1;i<=n;i++) cur[i]=point[i];pos[1]=++sz; belong[1]=1; st[++top]=1;while (top){int x=st[top];if (!use[x]){use[x]=1;if (son[x]){pos[son[x]]=++sz; belong[son[x]]=belong[x]; st[++top]=son[x];}continue;}while (cur[x]&&(v[cur[x]]==fa[x]||v[cur[x]]==son[x])) cur[x]=next[cur[x]];if (!cur[x]) {top--; continue;}int t=v[cur[x]];pos[t]=++sz; belong[t]=t; st[++top]=t;cur[x]=next[cur[x]];}}void update(int now){tr[now]=max(tr[now<<1],tr[now<<1|1]);}void pushdown(int x){if (!delta[x]) return;delta[x<<1]+=delta[x];delta[x<<1|1]+=delta[x];tr[x<<1]+=delta[x]; tr[x<<1|1]+=delta[x];delta[x]=0;}void query(int now,int l,int r,int ll,int rr){if (l>=ll&&r<=rr){tr[now]+=1;delta[now]+=1;return;}pushdown(now);int mid=(l+r)/2;if (ll<=mid) query(now<<1,l,mid,ll,rr);if (rr>mid) query(now<<1|1,mid+1,r,ll,rr);update(now);}void solve(int x,int y){while (belong[x]!=belong[y]) { if (deep[belong[x]]<deep[belong[y]]) swap(x,y); query(1,1,n,pos[belong[x]],pos[x]); x=fa[belong[x]]; }if (deep[x]>deep[y]) swap(x,y);query(1,1,n,pos[x],pos[y]);}int main(){scanf("%d%d",&n,&m);for (int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y); } dfs(); dfs2(); for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); solve(x,y); } printf("%d\n",tr[1]);}
- bzoj 4390: [Usaco2015 dec]Max Flow(树链剖分+手写栈)
- BZOJ 4390: [Usaco2015 dec]Max Flow|树链剖分
- BZOJ 4390: [Usaco2015 dec]Max Flow
- BZOJ 4390: [Usaco2015 dec]Max Flow 树链剖分/树上差分
- 【bzoj 4390】 [Usaco2015 dec]Max Flow(树上差分)
- 【BZOJ】4390 [Usaco2015 dec]Max Flow LCA+树上差分
- [Usaco2015 dec]Max Flow
- bzoj4390【Usaco2015 Dec】Max Flow
- 【bzoj4390】[Usaco2015 dec]Max Flow
- bzoj4390: [Usaco2015 dec]Max Flow
- [BZOJ4390][Usaco2015 dec]Max Flow(树上差分+lca)
- BZOJ 4397: [Usaco2015 dec]Breed Counting|暴力
- bzoj 4396: [Usaco2015 dec]High Card Wins
- bzoj 4397: [Usaco2015 dec]Breed Counting 乱搞
- USACO 2015 Dec Platinum 1.Max Flow
- BZOJ 4396: [Usaco2015 dec]High Card Wins|贪心
- BZOJ 4391/Luogu3129[Usaco2015 dec]High Card Low Card
- bzoj 4397: [Usaco2015 dec]Breed Counting 前缀和/线段树
- 面向项目(九)—— 交叉工具链
- 国内云建设普遍缺失的一环:云管理平台
- Deep Learning学习 之 卷积神经网络(CNN)
- “Java语言中的方法属于类中的成员(member)”为什么不对
- iOS开发之AFNetworking 3.0.4使用
- bzoj 4390: [Usaco2015 dec]Max Flow(树链剖分+手写栈)
- public static void main(String[] args)
- 【学习】AndroidStudio使用
- javascript中的显示和隐藏(display属性)
- JavaScript函数传参为EL表达式时
- eerTyraniBafonoitazilaireSredroerPyfireV.331
- Permutation I
- centos装aptget
- 基于腾讯云的Centos6系统搭建Apache+Mysql+PHP开发环境