BZOJ4390Max Flow

4390: [Usaco2015 dec]Max Flow
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 88 Solved: 55
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
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
Sample Output
9
Source
Platinum鸣谢Claris提供译文
本来是道链剖傻逼题。。
结果我TMpaint写错了。。
导致5A。。
我是渣渣。。
附上本蒟蒻的代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<climits>
using namespace std;
#define MAXN 50001
int n,m,cnt,sz,h[MAXN],father[MAXN][17],deep[MAXN],size[MAXN],pos[MAXN],belong[MAXN],delta[MAXN<<2];
bool vis[MAXN];
struct data
{
    int to,next;
}edge[MAXN<<1];
struct kx
{
    int value;
}node[MAXN<<2];

int read()
{
    int w=0,c=1; char ch=getchar();
    while (ch<'0' || ch>'9')
      {
        if (ch=='-') c=-1;
        ch=getchar();
      }
    while (ch>='0' && ch<='9')
      w=w*10+ch-'0',ch=getchar();
    return w*c;
}

void add(int u,int v)
{
    cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v;
    cnt++,edge[cnt].next=h[v],h[v]=cnt,edge[cnt].to=u;
}

void dfs1(int x)
{
    int i;
    size[x]=1,vis[x]=true;
    for (i=1;i<=16;i++)
      {
        if (deep[x]<(1<<i)) break;
        father[x][i]=father[father[x][i-1]][i-1];
      }
    for (i=h[x];i;i=edge[i].next)
      {
        if (vis[edge[i].to]) continue;
        deep[edge[i].to]=deep[x]+1,father[edge[i].to][0]=x;
        dfs1(edge[i].to),size[x]+=size[edge[i].to];
      }
}

void dfs2(int x,int chain)
{
    int k=0,i;
    sz++,pos[x]=sz,belong[x]=chain;
    for (i=h[x];i;i=edge[i].next)
      if (deep[edge[i].to]>deep[x] && size[edge[i].to]>size[k])
        k=edge[i].to;
    if (!k) return;
    dfs2(k,chain);
    for (i=h[x];i;i=edge[i].next)
      if (deep[edge[i].to]>deep[x] && k!=edge[i].to)
        dfs2(edge[i].to,edge[i].to);
}

void update(int s)
{
    node[s].value=max(node[s*2].value,node[s*2+1].value);
}

void build(int s,int l,int r)
{
    int mid=(l+r)/2;
    if (l==r) return;
    build(s*2,l,mid);
    build(s*2+1,mid+1,r);
    update(s);
}

void paint(int s,int z,int l,int r)
{
    node[s].value+=z;
    delta[s]+=z;
}

void pushdown(int s,int l,int r)
{
    int mid=(l+r)/2;
    paint(s*2,delta[s],l,mid);
    paint(s*2+1,delta[s],mid+1,r);
    delta[s]=0;
}

void insert(int s,int l,int r,int x,int y,int z)
{
    int mid=(l+r)/2;
    if (x<=l && y>=r)
      {
        paint(s,z,l,r);
        return;
      }
    if (delta[s]) pushdown(s,l,r);
    if (x<=mid) insert(s*2,l,mid,x,y,z);
    if (y>mid) insert(s*2+1,mid+1,r,x,y,z);
    update(s);
}

void solveinsert(int x,int y,int z)
{
    for (;belong[x]!=belong[y];x=father[belong[x]][0])
      {
        if (deep[belong[x]]<deep[belong[y]]) swap(x,y);
        insert(1,1,n,pos[belong[x]],pos[x],z);
      }
    if (deep[x]<deep[y]) swap(x,y);
    insert(1,1,n,pos[y],pos[x],z);
}

int querymax(int s,int l,int r,int x,int y)
{
    int mid=(l+r)/2,ans;
    if (x<=l && y>=r) return node[s].value;
    if (delta[s]) pushdown(s,l,r);
    if (x<=mid) ans=querymax(s*2,l,mid,x,y);
    else ans=-INT_MAX;
    if (y>mid) ans=max(ans,querymax(s*2+1,mid+1,r,x,y));
    update(s);
    return ans;
}

int main()
{
    int i,x,y;
    n=read(),m=read();
    for (i=1;i<n;i++)
      x=read(),y=read(),add(x,y);
    dfs1(1),dfs2(1,1),build(1,1,n);
    for (i=1;i<=m;i++)
      {
        x=read(),y=read();
        solveinsert(x,y,1);
      }
    printf("%d\n",querymax(1,1,n,1,n));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值