QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#457429 | #898. 二分图最大匹配 | 251Sec | TL | 0ms | 0kb | C++14 | 1.4kb | 2024-06-29 12:09:59 | 2024-06-29 12:09:59 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int S = 1, T = 2;
#define PC (ln + rn + 2)
int ln, rn, m;
int L(int x) { return x + 2; }
int R(int x) { return x + ln + 2; }
struct Edge {
int v, nxt, w;
} e[1000005];
int head[200005], len = 1;
void Insert(int u, int v, int w) {
e[++len] = { v, head[u], w };
head[u] = len;
}
void InsertF(int u, int v, int w) { Insert(u, v, w), Insert(v, u, 0); }
int cur[200005], dis[200005], gap[200005];
int DFS(int u, int rest) {
if (u == T) return rest;
int res = 0;
for (int &i = cur[u]; i; i = e[i].nxt) {
int v = e[i].v;
if (dis[u] != dis[v] + 1 || !e[i].w) continue;
int x = DFS(v, min(rest, e[i].w));
e[i].w -= x, e[i ^ 1].w += x;
rest -= x, res += x;
if (!rest) return res;
}
cur[u] = head[u];
if (!--gap[dis[u]]) dis[S] = PC;
gap[++dis[u]]++;
return res;
}
int Dinic() {
int res = 0;
while (dis[S] < PC) res += DFS(S, 1e9);
return res;
}
int main() {
scanf("%d%d%d", &ln, &rn, &m);
for (int i = 1; i <= m; i++) {
int a, b; scanf("%d%d", &a, &b);
InsertF(L(a + 1), R(b + 1), 1);
}
for (int i = 1; i <= ln; i++) InsertF(S, L(i), 1);
for (int i = 1; i <= rn; i++) InsertF(R(i), T, 1);
printf("%lld\n", Dinic());
for (int i = 2; i <= m * 2; i += 2) {
if (!e[i].w) {
int u = e[i ^ 1].v, v = e[i].v;
printf("%d %d\n", u - 3, v - 3 - ln);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
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...