题目
https://www.luogu.org/problem/show?pid=1345
题解
一开始我是这样想的:直接把所有边的容量建成1然后最小割就是答案。因为最小割的定义就是割去容量和最小的边使得S和T不连通,把所有边的容量设为1,最小割不就是答案吗??结果WA了。
这道题是要歌曲最少的点,而我成了割去最少的边。。。。但我又考虑,一个大小为1的流不正好对应应该去掉一个点吗??但是又一想,发现如下情况:
这样中间那个点就被流过了两次,也就是割了两次,为了使一个点只被割一次,拆点即可。
代码
//最小割
#include <cstdio>
#include <algorithm>
#define inf 0x3f3f3f3f
#define maxn 100000
using namespace std;
int N, M, next[maxn], head[maxn], c[maxn], num[maxn], to[maxn], d[maxn], last[maxn],
tot=1, Exit, S, T;
void adde(int a, int b, int v){to[++tot]=b;c[tot]=v;next[tot]=head[a];head[a]=tot;}
void adde2(int a, int b, int v){adde(a,b,v),adde(b,a,0);}
int ISAP(int pos, int in)
{
int flow=0, t, p;
if(pos==T)return in;
for(p=last[pos];p;last[pos]=p=next[p])
if(c[p] and d[to[p]]+1==d[pos])
{
flow+= t=ISAP(to[p],min(c[p],in-flow));
c[p]-=t, c[p xor 1]+=t;
if(Exit or in==flow)return flow;
}
Exit=--num[d[pos]]==0;
++num[++d[pos]];
last[pos]=head[pos];
return flow;
}
void init()
{
int i, x, y;
scanf("%d%d%d%d",&N,&M,&S,&T);S+=N;
for(i=1;i<=N;i++)adde2(i,i+N,1);
for(i=1;i<=M;i++)scanf("%d%d",&x,&y),adde2(x+N,y,1),adde2(y+N,x,1);
}
int main()
{
int ans=0;
init();
while(!Exit)ans+=ISAP(S,inf);
printf("%d",ans);
return 0;
}