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 93
| #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #define MAXN 1000002 #define INF 2000000000 int min(int a, int b) { if (a < b) return a; else return b; } struct Edge { int v, nx, w; } e[MAXN]; std::queue <int> q; int n, m, head[MAXN], ecnt = 1, x, y, z, r, k, dep[MAXN], cur[MAXN], cnt = 1, totp = 0, tot, c, p, g; void add(int f, int t, int w) { e[++ecnt] = (Edge) {t, head[f], w}; head[f] = ecnt; e[++ecnt] = (Edge) {f, head[t], 0}; head[t] = ecnt; } bool bfs(int s, int t) { memset(dep, 0x7f, sizeof(dep)); while (!q.empty()) q.pop(); for (int i = 1; i <= n + 2; i++) { cur[i] = head[i]; } dep[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int i = head[v]; i; i = e[i].nx) { int to = e[i].v; if (dep[to] > INF && e[i].w) { dep[to] = dep[v] + 1; q.push(to); } } } if (dep[t] < INF) return 1; else return 0; } int dfs(int u, int t, int l) { if (!l || u == t) return l; int fl = 0, f; for (int i = cur[u]; i; i = e[i].nx) { cur[u] = i; int to = e[i].v; if (dep[to] == dep[u] + 1 && (f = dfs(to, t, min(l, e[i].w)))) { fl += f; l -= f; e[i ^ 1].w += f; e[i].w -= f; if (!l) break; } } return fl; } int Dinic(int s, int t) { int maxf = 0; while (bfs(s, t)) { maxf += dfs(s, t , INF); } return maxf; } int h(int i, int j) { return (i - 1) * m + j; } char a, b; int main() { scanf("%d%d", &n, &m); r = n + 1; k = n + 2; for (int i = 1; i <= n; i++) { scanf("%d", &x); if (x == 1) { add(r, i, 1); } else { add(i, k, 1); } } for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); add(x, y, 1); add(y, x, 1); } tot = Dinic(r, k); printf("%d\n", tot); return 0; }
|