1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <iostream> #include <cstring> #include <algorithm>
#define debug
using namespace std;
const int N = 55 << 1, M = (N * N) << 1, INF = 0x3f3f3f3f; int n, m, S, T; int h[N], e[M], ne[M], f[M], fs[M], idx; bool g[N][N]; int q[N], cur[N], d[N];
void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], f[idx] = c, h[a] = idx ++; e[idx] = a, ne[idx] = h[b], f[idx] = 0, h[b] = idx ++; }
bool bfs() { int hh = 0, tt = 0; memset(d, -1, sizeof d); q[0] = S, d[S] = 0, cur[S] = h[S]; while (hh <= tt) { int t = q[hh ++]; for (int i = h[t]; ~i; i = ne[i]) { int ver = e[i]; if (d[ver] == -1 && f[i]) { d[ver] = d[t] + 1; cur[ver] = h[ver]; if (ver == T) return true; q[++ tt] = ver; } } } return false; }
int find(int u, int limit) { if (u == T) return limit; int flow = 0; for (int i = cur[u]; ~i && flow < limit; i = ne[i]) { cur[u] = i; int ver = e[i]; if (d[ver] == d[u] + 1 && f[i]) { int t = find(ver, min(f[i], limit - flow)); if (!t) d[ver] = -1; f[i] -= t, f[i ^ 1] += t, flow += t; } } return flow; }
int dinic() { int r = 0, flow; while (bfs()) while (flow = find(S, INF)) r += flow; return r; }
void init() { memset(h, -1, sizeof h); memset(g, 0, sizeof g); memset(f, 0, sizeof f); idx = 0; }
int main() { while (scanf("%d%d", &n, &m) != EOF) { init();
for (int i = 1; i <= m; ++ i) { int a, b; scanf(" (%d,%d)", &a, &b); a ++, b ++; add(a + n, b, INF); add(b + n, a, INF); g[a][b] = g[b][a] = true; } for (int i = 1; i <= n; ++ i) add(i, i + n, 1); memcpy(fs, f, sizeof f);
int res = n; for (S = n + 1; S <= 2 * n; ++ S) for (T = S - n + 1; T <= n; ++ T) if (!g[S - n][T]) { memcpy(f, fs, sizeof f); res = min(res, dinic()); }
printf("%d\n", res); } }
|