Max Flow

Max Flow

题目描述

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 K pairs of stalls (1≤K≤100,000). For the ith such pair, you are told two stalls si and ti, 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 K 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 si to ti, then it counts as being pumped through the endpoint stalls si and ti, as well as through every stall along the path between them.

输入

The first line of the input contains N and K.

The next N−1 lines each contain two integers x and y (x≠y) describing a pipe between stalls x and y.

The next K lines each contain two integers s and t describing the endpoint stalls of a path through which milk is being pumped.

输出

An integer specifying the maximum amount of milk pumped through any stall in the barn.

样例输入

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分析:Tarjan+差分思想;   难点在于怎么处理树上的区间加减问题;   把数列差分思想用到树上,区间[i~j]加上k,等价于a[i]+=k,a[j+1]-=k,ans[t]=Σa[p](1=<p<=t);   树上的s,t区间分成两个,设s,t公共祖先为p,一个是s到p的儿子,另一个是t到p,两个区间都差分一下;   最后dfs递归处理类似前缀和的答案;代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,fa[maxn],ans[maxn],vis[maxn],faa[maxn],ma;
vi a[maxn],query[maxn];
int find(int x)
{
    return faa[x]==x?x:faa[x]=find(faa[x]);
}
void dfs(int now,int pre)
{
    for(int x:a[now])
    {
        if(x!=pre)
        {
            dfs(x,now);
            fa[x]=now;
        }
    }
}
void dfs1(int now)
{
    vis[now]=1;
    for(int x:query[now])
    {
        if(vis[x])
        {
            int p=find(x);
            ans[p]--,ans[fa[p]]--;
        }
    }
    for(int x:a[now])
    {
        if(!vis[x])
        {
            dfs1(x);
            faa[x]=now;
        }
    }
}
void dfs2(int now,int pre)
{
    for(int x:a[now])
    {
        if(x!=pre)
        {
            dfs2(x,now);
            ans[now]+=ans[x];
        }
    }
    ma=max(ma,ans[now]);
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&k);
    rep(i,1,n)faa[i]=i;
    rep(i,1,n-1)
    {
        scanf("%d%d",&j,&t);
        a[j].pb(t),a[t].pb(j);
    }
    rep(i,1,k)
    {
        scanf("%d%d",&j,&t);
        ans[j]++,ans[t]++;
        query[j].pb(t);
        query[t].pb(j);
    }
    dfs(1,-1);
    dfs1(1);
    dfs2(1,-1);
    printf("%d\n",ma);
    //system("pause");
    return 0;
}
时间: 2024-10-13 12:22:08

Max Flow的相关文章

bzoj4390: [Usaco2015 dec]Max Flow

4390: [Usaco2015 dec]Max Flow Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 139  Solved: 85[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), co

洛谷 P3128 [USACO15DEC]最大流Max Flow

P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1…N. Each pipe connects a pair of stalls,

洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk

洛谷P3128 [USACO15DEC]最大流Max Flow

洛谷P3128 [USACO15DEC]最大流Max Flow 题目描述 FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道,隔间编号从1到N.所有隔间都被管道连通了. FJ有K(1≤K≤100,000)条运输牛奶的路线,第i条路线从隔间si运输到隔间ti.一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个单位的运输压力,你需要计算压力最大的隔间的压力是多少. 输入输出格式 输入格式: The first line of the input contains

graph | Max flow

最大流算法,解决的是从一个起点到一个终点,通过任何条路径能够得到的最大流量. 有个Edmond-Karp算法: 1. BFS找到一条增广路径:算出这条路径的最小流量(所有边的最小值)increase: 2. 然后更新路径上的权重(流量),正向边加上increase,反向边减去increase; 3. 重复1,直到没有增广路径: 至于为什么要用反向边,这里讲得挺清楚: 1 #include <iostream> 2 #include <stdio.h> 3 #include <

P3128 [USACO15DEC]最大流Max Flow

题目描述 Farmer John has installed a new system of N-1N?1 pipes to transport milk between the NN stalls in his barn (2 \leq N \leq 50,0002≤N≤50,000), conveniently numbered 1 \ldots N1-N. Each pipe connects a pair of stalls, and all stalls are connected t

洛谷——P3128 [USACO15DEC]最大流Max Flow

https://www.luogu.org/problem/show?pid=3128 题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-

AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk

洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk