[Usaco2015 Dec] Max Flow

Source: Internet
Author: User

[Usaco2015 Dec] Max Flow
Description

Farmer John has installed a new system of N? 1 pipes to transport milk between the N stils in his barn (2 ≤ N ≤ 50,000), conveniently numbered 1... N. Each pipe connects a pair of stils, and all stils are connected to each-other via paths of pipes.

FJ is pumping milk between KK pairs of stils (1 ≤ K ≤ 100,000 ). for the iith such pair, you are told two stils sisi and titi, endpoints of a path along which milk is being pumped at a unit rate. FJ is concerned that some stles might end up overwhelmed with all the milk being pumped through them, since a stall can serve as a waypoint along serving 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 stils sisi and titi, as well as through every stall along the path between them.

Given a tree with N vertices, the weights of all nodes are 0.

There are K operations. specify two vertices s, t each time, and add the weights of all vertices from s to t path.

Output the value of the vertex with the largest weight value after K operations.

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 stils x and y.

The next K lines each contain two integers ss and t describing the endpoint stballs 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 Input5 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 Output9HINTSource
#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include#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<
      
       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]) 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;}
       
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.