4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 228 Solved: 143
[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
打了链剖
看看别人的题解 树上差分 lca-- fa(lca)-- u++ v++
涨姿势了
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
inline void print(ll x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=50100,inf=0X3f3f3f3f;
int n;
int ecnt,last[N];
struct EDGE{int to,nt;}e[N<<1];
inline void add(int u,int v)
{e[++ecnt]=(EDGE){v,last[u]};last[u]=ecnt;}
int fa[N],d[N],top[N],dfn[N],size[N],cnt;
void dfs1(int u)
{
register int i;size[u]=1;
for(i=last[u];i;i=e[i].nt)
{
if(fa[u]==e[i].to)continue;
fa[e[i].to]=u;d[e[i].to]=d[u]+1;
dfs1(e[i].to);size[u]+=size[e[i].to];
}
}
void dfs2(int u,int tp)
{
top[u]=tp;dfn[u]=++cnt;
register int i,k=0;
for(i=last[u];i;i=e[i].nt)
{
if(e[i].to==fa[u])continue;
if(size[e[i].to]>size[k])k=e[i].to;
}
if(!k)return ;
dfs2(k,tp);
for(i=last[u];i;i=e[i].nt)
{
if(e[i].to==fa[u]||e[i].to==k)continue;
dfs2(e[i].to,e[i].to);
}
}
struct seg_tree{int l,r,mx,mark;}tr[N<<2];
void build(int k,int l,int r)
{
tr[k].l=l;tr[k].r=r;
if(l==r)return ;
int mid=l+r>>1;
build(k<<1,l,mid);build(k<<1|1,mid+1,r);
}
inline void pushup(int k)
{tr[k].mx=max(tr[k<<1].mx,tr[k<<1|1].mx);}
inline void pushdown(int k)
{
if(tr[k].mark)
{
tr[k<<1].mark+=tr[k].mark;
tr[k<<1|1].mark+=tr[k].mark;
tr[k<<1].mx+=tr[k].mark;
tr[k<<1|1].mx+=tr[k].mark;
tr[k].mark=0;
}
}
void modify(int k,int x,int y)
{
int l=tr[k].l,r=tr[k].r;
if(l>=x&&r<=y)
{tr[k].mark++;tr[k].mx++;return;}
int mid=l+r>>1;pushdown(k);
if(mid>=x)modify(k<<1,x,y);
if(mid<y)modify(k<<1|1,x,y);
pushup(k);
}
void tree_plus(int u,int v)
{
while(top[u]^top[v])
{
if(d[top[u]]<d[top[v]])swap(u,v);
modify(1,dfn[top[u]],dfn[u]);u=fa[top[u]];
}
if(dfn[u]>dfn[v])swap(u,v);
modify(1,dfn[u],dfn[v]);
}
int main()
{
n=read();int Q=read();
register int i,u,v;
for(i=1;i<n;++i)
{u=read();v=read();add(u,v);add(v,u);}
dfs1(1);dfs2(1,1);
build(1,1,n);
while(Q--)
{
u=read();v=read();
tree_plus(u,v);
}
print(tr[1].mx);puts("");return 0;
}
/*
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
*/