QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#340586 | #898. 二分图最大匹配 | FISHER_# | RE | 0ms | 0kb | C++14 | 1.6kb | 2024-02-29 10:29:36 | 2024-02-29 10:29:38 |
answer
#include <bits/stdc++.h>
#define PB push_back
#define EB emplace_back
using namespace std;
const int maxn = 100;
int S, T;
struct edge { int v, f; };
vector<edge> e;
vector<int> g[maxn + 5];
inline void addE(int u, int v, int w) {
g[u].PB(e.size()), e.PB({v, w});
g[v].PB(e.size()), e.PB({u, 0});
}
int d[maxn + 5];
int cur[maxn + 5];
bool bfs(int s, int t) {
memset(d, 127, sizeof(d));
memset(cur, 0, sizeof(cur));
queue<int> q;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int eid : g[u]) {
int v = e[eid].v, f = e[eid].f;
if (d[v] > T && f) {
d[v] = d[u] + 1;
if (v == t) return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int u, int t, int fl) {
if (u == t) return fl;
int rs = 0;
for (int i = cur[u]; i < g[u].size(); i++) {
cur[u] = i;
int eid = g[u][i];
int v = e[eid].v, f = e[eid].f;
if (d[v] == d[u] + 1 && f) {
int nf = dfs(v, t, min(fl, f));
fl -= nf, rs += nf;
e[eid].f -= nf, e[eid ^ 1].f += nf;
}
if (!fl) break;
}
return rs;
}
int dinic(int s, int t) {
int rs = 0;
while (bfs(s, t)) rs += dfs(s, t, INT_MAX);
return rs;
}
int main() {
int l, r, m;
scanf("%d%d%d", &l, &r, &m);
S = l + r + 1, T = S + 1;
for (int i = 1; i <= l; i++) addE(S, i, 1);
for (int i = 1; i <= r; i++) addE(l + i, T, 1);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
addE(u + 1, v + l + 1, 1);
}
printf("%d\n", dinic(S, T));
for (int i = 1; i <= l; i++)
for (int eid : g[i]) {
int v = e[eid].v, f = e[eid].f;
if (v != S && !f) printf("%d %d\n", i - 1, v - l - 1);
}
}
详细
Test #1:
score: 0
Runtime Error
input:
100000 100000 200000 78474 45795 32144 46392 92549 13903 73460 34144 96460 92850 56318 77066 77529 84436 76342 51542 77506 99268 76410 89381 1778 61392 43607 96135 84268 74827 14857 35966 32084 94908 19876 174 1481 94390 12423 55019 64368 92587 81295 7902 25432 46032 36293 61128 73555 84836 8418 102...