bzoj4390 Max Flow

来源:互联网 发布:mac没有host 编辑:程序博客网 时间:2024/06/08 15:34

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.

对每条路径求lca打标记,最后dfs一遍统计。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int fir[50010],ne[100010],to[100010],fa[50010][20],tag[50010],dep[50010],n,m,ans=-1;void add(int num,int u,int v){    ne[num]=fir[u];    fir[u]=num;    to[num]=v;}void dfs1(int u){    int v;    for (int i=fir[u];i;i=ne[i])        if ((v=to[i])!=fa[u][0])        {            fa[v][0]=u;            dep[v]=dep[u]+1;            dfs1(v);        }}int lca(int u,int v){    if (dep[u]<dep[v]) swap(u,v);    for (int k=17;k>=0;k--)        if (dep[u]-(1<<k)>=dep[v]) u=fa[u][k];    if (u==v) return u;    for (int k=17;k>=0;k--)        if (fa[u][k]!=fa[v][k])        {            u=fa[u][k];            v=fa[v][k];        }    return fa[u][0];}void dfs2(int u){    int v;    for (int i=fir[u];i;i=ne[i])        if ((v=to[i])!=fa[u][0])        {            dfs2(v);            tag[u]+=tag[v];        }    ans=max(ans,tag[u]);}int main(){    int u,v,x;    scanf("%d%d",&n,&m);    for (int i=1;i<n;i++)    {        scanf("%d%d",&u,&v);        add(i*2,u,v);        add(i*2+1,v,u);    }    dep[1]=1;    dfs1(1);    for (int k=1;k<=17;k++)        for (int i=1;i<=n;i++)            fa[i][k]=fa[fa[i][k-1]][k-1];    while (m--)    {        scanf("%d%d",&u,&v);        x=lca(u,v);        tag[u]++;        tag[v]++;        tag[x]--;        tag[fa[x][0]]--;    }    dfs2(1);    printf("%d\n",ans);}
0 0
原创粉丝点击