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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #define INF (0x7fffffff) #define MAXN (100000 + 5) #define MAXM (500000 + 5) #define rev(i) ((((i) - 1) ^ 1) + 1) #define cbh(i) ((i) + 10000) using namespace std; int s = 0, t = MAXN - 1; struct edg { int from, to, next, cap, flow; }b[MAXM << 1]; int g[MAXN], cntb, n, m, dis[MAXN]; void adn(int from, int to, int cap) { b[++cntb].next = g[from]; b[cntb].from = from, b[cntb].to = to, b[cntb].cap = cap, b[cntb].flow = 0; g[from] = cntb; } bool bfs() { queue<int> q; q.push(s); memset(dis, 0, sizeof(dis)); dis[s] = 1; while (!q.empty()) { int dq = q.front(); q.pop(); for (int i = g[dq]; i; i = b[i].next) if (b[i].cap > b[i].flow && !dis[b[i].to]) { dis[b[i].to] = dis[dq] + 1; q.push(b[i].to); } } return dis[t] > 0; } int dinic(int dq, int maxf) { if (dq == t || !maxf) return maxf; int re = 0; for (int i = g[dq]; i; i = b[i].next) if (b[i].cap > b[i].flow && dis[dq] + 1 == dis[b[i].to]) { int zl = dinic(b[i].to, min(maxf, b[i].cap - b[i].flow)); re += zl; maxf -= zl; b[i].flow += zl; b[rev(i)].flow -= zl; } return re; } int solve() { int re = 0; while (bfs()) re += dinic(s, INF); return re; } void ef() { int le = 0, ri = m + 1, ans; while (le < ri) { int mi = (le + ri) >> 1; for (int i = 1; i <= cntb; i++) b[i].flow = 0; for (int i = g[s]; i; i = b[i].next) b[i].cap = mi; if (solve() >= m) ans = mi, ri = mi; else le = mi + 1; } printf("%d\n", ans); } int main() { scanf("%d%d", &n, &m); for (int i = 1, srx, sry; i <= m; i++) { scanf("%d%d", &srx, &sry); adn(srx, cbh(i), 1); adn(cbh(i), srx, 0); adn(sry, cbh(i), 1); adn(cbh(i), sry, 0); adn(cbh(i), t, 1); adn(t, cbh(i), 0); } for (int i = 1; i <= n; i++) adn(s, i, 1), adn(i, s, 0); ef(); return 0; }
|