USACO Max Flow

Luo Gu P3128 [USACO15DEC] maximum flow Max Flow

Luo Gu Portal

JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow

JDOJ Portal

Description

Farmer John has installed a new system of N−1pipes to transport milk between the ), conveniently numbered. Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes.

Input

The first line of the input contains Nand.

The next K lines each contain two integers s 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

2015~2016

Subject to the effect:

FJ to his barn N installed between N-1 of conduits (2≤N≤50,000) compartments, the compartments are numbered from 1 to N. All the compartments are in communication pipe.

There FJ K (1≤K≤100,000) milk transport route, a route leading from the i-th compartment to compartment si ti. Pressure a transport route will give its compartment and all the compartments of the middle route of two endpoints to bring pressure on the transportation of a unit, you need to calculate the maximum pressure compartment is.

answer:

Tree chain split template class title.

But this turned into a range of modified (+1), and the maximum maintenance of the entire range.

So we write the tree line when we must pay attention to the wording of the correlation function is no longer a sum of the segment tree wording.

We maintain segment tree node represents the maximum interval with an array. Then we hit the mark in the pushdown lazy when it can only + = k, rather than + = (r-l + 1) * k.

Principle is very simple, if you modify the current range of all-inclusive range, then the current maximum range of the array segment tree maintenance can add 1 as its left and right son son all plus 1, the final statistics did not answer the slightest impact .

Code:

#include<cstdio>
#include<algorithm>
#define lson pos<<1
#define rson pos<<1|1
using namespace std;
const int maxn=50001;
int n,k,tot,cnt;
int head[maxn],nxt[maxn<<1],to[maxn<<1];
int deep[maxn],size[maxn],son[maxn],fa[maxn];
int top[maxn],id[maxn];
int fuck[maxn<<2],lazy[maxn<<2],sum[maxn<<2];
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
    int x=0,f=1;
    char ch=nc();
    while(ch<48){if(ch=='-') f=-1;ch=nc();}
    while(ch>=48)   x=x*10+ch-'0',ch=nc();
    return x*f;
}
void add(int x,int y)
{
    to[++tot]=y;
    nxt[tot]=head[x];
    head[x]=tot;
}
void dfs1(int x,int f)
{
    deep[x]=deep[f]+1;
    fa[x]=f;
    size[x]=1;
    for(int i=head[x];i;i=nxt[i])
    {
        int y=to[i];
        if(y==f)
            continue;
        dfs1(y,x);
        size[x]+=size[y];
        if(!son[x]||size[y]>size[son[x]])
            son[x]=y;
    }
}
void dfs2(int x,int t)
{
    id[x]=++cnt;
    top[x]=t;
    if(!son[x])
        return;
    dfs2(son[x],t);
    for(int i=head[x];i;i=nxt[i])
    {
        int y=to[i];
        if(y==fa[x]||y==son[x])
            continue;
        dfs2(y,y);
    }
}
void mark(int pos,int l,int r,int k)
{
    sum[pos]+=k;
    lazy[pos]+=k;
}
void pushdown(int pos,int l,int r)
{
    int mid=(l+r)>>1;
    mark(lson,l,mid,lazy[pos]);
    mark(rson,mid+1,r,lazy[pos]);
    lazy[pos]=0;
}
void update(int pos,int l,int r,int x,int y,int k)
{ 
    int mid=(l+r)>>1;
    if(x<=l && r<=y)
    {
        mark(pos,l,r,k);
        return;
    }
    pushdown(pos,l,r);
    if(x<=mid)
        update(lson,l,mid,x,y,k);
    if(y>mid)
        update(rson,mid+1,r,x,y,k);
    sum[pos]=max(sum[lson],sum[rson]);
}
void upd_chain(int x,int y,int k)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]])
            swap(x,y);
        update(1,1,n,id[top[x]],id[x],k);
        x=fa[top[x]];
    }
    if(deep[x]<deep[y])
        swap(x,y);
    update(1,1,n,id[y],id[x],k);
}
int main()
{
    n=read();k=read();
    for(int i=1;i<n;i++)
    {
        int x,y;
        x=read();y=read();
        add(x,y);
        add(y,x);
    }
    dfs1(1,0);
    dfs2(1,1);
    while(k--)
    {
        int x,y;
        x=read();y=read();
        upd_chain(x,y,1);
    }
    printf("%d",sum[1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11583038.html