裸的最小割,拆点时要考虑清楚到底是怎么连
如果之前i->i+n,之后又x->y+n,显然出不了解
所以可以改为i+n->i
如果要输出方案
考虑每一个一个点,如果删去这个点,最小割变小了,那么这个点就是所求点。
不输出方案的代码
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(210), __(1e5 + 10), INF(2147483647);
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, w[__], fst[_], nxt[__], to[__], cnt;
int S, T, lev[_], cur[_], max_flow;
queue <int> Q;
IL void Add(RG int u, RG int v, RG int f){
w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}
IL int Dfs(RG int u, RG int maxf){
if(u == T) return maxf;
RG int ret = 0;
for(RG int &e = cur[u]; e != -1; e = nxt[e]){
if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
RG int f = Dfs(to[e], min(w[e], maxf - ret));
ret += f; w[e ^ 1] += f; w[e] -= f;
if(ret == maxf) break;
}
return ret;
}
IL bool Bfs(){
Fill(lev, 0); lev[S] = 1; Q.push(S);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(lev[to[e]] || !w[e]) continue;
lev[to[e]] = lev[u] + 1;
Q.push(to[e]);
}
}
return lev[T];
}
int main(RG int argc, RG char* argv[]){
n = Read(); m = Read(); S = Read(); T = Read() + n; Fill(fst, -1);
for(RG int i = 1; i <= n; i++) Add(i + n, i, 1);
for(RG int i = 1, a, b; i <= m; i++) a = Read(), b = Read(), Add(a, b + n, 1), Add(b, a + n, 1);
while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
printf("%d
", max_flow);
return 0;
}