P1345 [USACO5.4]奶牛的电信Telecowmunication
一开始看到这题懵逼了,后来知道有最大流最小割定理…暂且就先这么直接用了
最小割=最少删掉多少条边可以使得两个点不连通=最大流
但是这题连边还很恶心,如果直接连容量为1的边,这题是做不出来的,因为每个点最多只能被删一次
所以需要一组[1,n]的点的复制标记为[n+1,n+k],这些点出去的边如果流量非0,则意味着该点该点-n对应的点需要删除
右边到左边的最大容量是1,如果流量是1,说明该点需要删除
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=3e3+7;
const int maxm=3e3+7;
const int INF=1e9;
const ll INFF=1e18;
struct Edge
{
int to,next,cap,flow;
}edge[maxm];
int tol,head[maxn];
void init()
{
tol=2;
mem(head,-1);
}
void add(int u,int v,int w,int rw=0)
{
edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
edge[tol].next=head[u];head[u]=tol++;
edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;
edge[tol].next=head[v];head[v]=tol++;
}
int Q[maxn],dep[maxn],cur[maxn],sta[maxn],n,m,u,v,sx,ex;
bool bfs(int sx,int ex,int n)
{
int front=0,tail=0;
mem(dep,-1);
dep[sx]=0;
Q[tail++]=sx;
while(front<tail)
{
int u=Q[front++];
for (int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if (edge[i].cap>edge[i].flow&&dep[v]==-1)
{
dep[v]=dep[u]+1;
if (v==ex)return true;
Q[tail++]=v;
}
}
}
return false;
}
int dinic(int sx,int ex,int n)
{
int maxflow=0;
while(bfs(sx,ex,n))
{
rep(i,0,2*n)cur[i]=head[i];
int u=sx,tail=0;
while(cur[sx]!=-1)
{
if (u==ex)
{
int tp=INF;
for (int i=tail-1;i>=0;i--)
{
tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
}
maxflow+=tp;
for (int i=tail-1;i>=0;i--)
{
edge[sta[i]].flow+=tp;
edge[sta[i]^1].flow-=tp;
if (edge[sta[i]].cap-edge[sta[i]].flow==0)tail=i;
}
u=edge[sta[tail]^1].to;
}
else if (cur[u]!=-1&&edge[cur[u]].cap>edge[cur[u]].flow&&dep[u]+1==dep[edge[cur[u]].to])
{
sta[tail++]=cur[u];
u=edge[cur[u]].to;
}
else
{
while(u!=sx&&cur[u]==-1)u=edge[sta[--tail]^1].to;
cur[u]=edge[cur[u]].next;
}
}
}
return maxflow;
}
int main()
{
init();
scanf("%d%d%d%d",&n,&m,&sx,&ex);
rep(i,1,n)add(n+i,i,1,0);
rep(i,1,m)
{
scanf("%d%d",&u,&v);
add(u,v+n,INF,0);
add(v,u+n,INF,0);
}
W(dinic(sx,ex+n,n));
return 0;
}