您的位置:首页 > 其它

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

2016-11-25 17:59 603 查看

题目描述

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 between   pairs of stalls ( ). For the  th such pair, you are told two stalls   and  , 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  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   to  , then it counts as being pumped through the endpoint stalls   and

, as well as through every stall along the path between them.

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   and  .

The next   lines each contain two integers   and   ( ) describing a pipe

between stalls   and  .

The next   lines each contain two integers   and   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.

 

输入输出样例

输入样例#1:
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
输出样例#1:
9

倍增LCA+树上差分。

树结点的权值等于其子树所有节点的差分结果。

704ms,不知道用树剖求LCA会有多快

/*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=50010;
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;
}
struct edge{
int v,nxt;
}e[mxn<<1];
int hd[mxn],mct=0;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,k;
int dep[mxn];
int fa[mxn][17];
int a[mxn];//差分
void DFS(int u,int f){
dep[u]=dep[f]+1;
for(int i=1;i<17;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
fa[v][0]=u;
DFS(v,u);
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=16;i>=0;i--){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i=16;i>=0;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
int ans=-1e9;
int clc(int u,int f){
int res=a[u];
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
res+=clc(v,u);
}
ans=max(ans,res);
return res;
}
int main(){
n=read();k=read();
int i,j,x,y;
for(i=1;i<n;i++){
x=read();y=read();
add_edge(x,y);
add_edge(y,x);
}
DFS(1,0);
for(i=1;i<=k;i++){
x=read();y=read();
a[x]++;a[y]++;
int tmp=LCA(x,y);
a[tmp]--;
if(fa[tmp][0])a[fa[tmp][0]]--;
}
clc(1,0);
printf("%d\n",ans);
return 0;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: