bzoj4390【Usaco2015 Dec】Max Flow
来源:互联网 发布:mac server如何使用 编辑:程序博客网 时间:2024/06/07 13:20
4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 56 Solved: 40
[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<cmath>#include<cstring>#include<cstdlib>#include<algorithm>#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define maxn 50005#define inf 1000000000using namespace std;struct edge_type{int to,next;}e[maxn*2];struct seg{int l,r,mx,tag;}t[maxn*4];int fa[maxn][20],d[maxn],size[maxn],head[maxn],pos[maxn],belong[maxn];int n,m,x,y,tmp,cnt,tot;bool vst[maxn];inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}inline void add_edge(int x,int y){e[++cnt]=(edge_type){y,head[x]};head[x]=cnt;e[++cnt]=(edge_type){x,head[y]};head[y]=cnt;}inline void dfs1(int x){size[x]=1;vst[x]=true;F(i,1,20){if (d[x]<(1<<i)) break;fa[x][i]=fa[fa[x][i-1]][i-1];}for(int i=head[x];i;i=e[i].next){int y=e[i].to;if (vst[y]) continue;d[y]=d[x]+1;fa[y][0]=x;dfs1(y);size[x]+=size[y];}}inline void dfs2(int x,int chain){int k=0;pos[x]=++tot;belong[x]=chain;for(int i=head[x];i;i=e[i].next)if (d[e[i].to]>d[x]&&size[e[i].to]>size[k]) k=e[i].to;if (k==0) return;dfs2(k,chain);for(int i=head[x];i;i=e[i].next)if (d[e[i].to]>d[x]&&k!=e[i].to) dfs2(e[i].to,e[i].to);}inline int lca(int x,int y){if (d[x]<d[y]) swap(x,y);int t=int(log2(d[x]-d[y]));D(i,t,0) if (d[x]-(1<<i)>=d[y]) x=fa[x][i];if (x==y) return x;t=int(log2(d[x]));D(i,t,0) if (fa[x][i]!=fa[y][i]){x=fa[x][i];y=fa[y][i];}return fa[x][0];}inline void pushup(int k){t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx);}inline void update(int k,int x){t[k].mx+=x;t[k].tag+=x;}inline void pushdown(int k){if (!t[k].tag) return;update(k<<1,t[k].tag);update(k<<1|1,t[k].tag);t[k].tag=0;}inline void build(int k,int l,int r){t[k].l=l;t[k].r=r;t[k].tag=t[k].mx=0;if (l==r) return;int mid=(l+r)>>1;build(k<<1,l,mid);build(k<<1|1,mid+1,r);}inline void add(int k,int l,int r,int x){if (t[k].l==l&&t[k].r==r){update(k,x);return;}int mid=(t[k].l+t[k].r)>>1;pushdown(k);if (r<=mid) add(k<<1,l,r,x);else if (l>mid) add(k<<1|1,l,r,x);else {add(k<<1,l,mid,x);add(k<<1|1,mid+1,r,x);}pushup(k);}inline void solveadd(int x,int f){while (belong[x]!=belong[f]){add(1,pos[belong[x]],pos[x],1);x=fa[belong[x]][0];}add(1,pos[f],pos[x],1);}int main(){n=read();m=read();F(i,1,n-1){x=read();y=read();add_edge(x,y);}dfs1(1);dfs2(1,1);build(1,1,n);F(i,1,m){x=read();y=read();tmp=lca(x,y);solveadd(x,tmp);solveadd(y,tmp);add(1,pos[tmp],pos[tmp],-1);}printf("%d\n",t[1].mx);return 0;}
- bzoj4390【Usaco2015 Dec】Max Flow
- 【bzoj4390】[Usaco2015 dec]Max Flow
- bzoj4390: [Usaco2015 dec]Max Flow
- [BZOJ4390][Usaco2015 dec]Max Flow(树上差分+lca)
- [Usaco2015 dec]Max Flow
- BZOJ 4390: [Usaco2015 dec]Max Flow|树链剖分
- BZOJ 4390: [Usaco2015 dec]Max Flow
- bzoj4390 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+树上差分
- USACO 2015 Dec Platinum 1.Max Flow
- USACO 2015 Dec Platinum 1.Max Flow题解,
- bzoj4394【Usaco2015 Dec】Bessie
- 【Usaco2015 dec 】Counting Haybales
- bzoj4397【Usaco2015 Dec】Breed Counting
- bzoj4393【Usaco2015 Dec】Fruit Feast
- android jaudiotagger读取mp3和Flac音频操作
- HP pavilion 安装ubuntu14.04后无线无法使用问题总结
- OkHttp的使用
- iOS 用for循环写了一排button
- HDU 2088 Calculate S(n)
- bzoj4390【Usaco2015 Dec】Max Flow
- 二分+贪心 Codeforces614D Skills
- 12306为什么买票这么难
- C语言之括号匹配
- Xcode 快捷键大全
- ListView 数据错乱问题的解决(CheckBox)
- UML类图几种关系的总结
- mysql 内存优化
- 恢复oracle被误更新数据的办法