QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390339 | #898. 二分图最大匹配 | iee | TL | 0ms | 0kb | C++23 | 1.7kb | 2024-04-15 12:18:27 | 2024-04-15 12:18:28 |
answer
#include <bits/stdc++.h>
using namespace std;
int S, T;
struct MaxFlow {
static constexpr int N = 2e5 + 55;
struct edge_t {
int v, f;
};
vector<edge_t> l;
vector<int> e[N];
void add(int u, int v, int f) {
e[u].push_back(l.size());
l.push_back({v, f});
e[v].push_back(l.size());
l.push_back({u, 0});
}
int dis[N], cur[N];
bool bfs() {
memset(dis, -1, sizeof(dis));
queue<int> Q;
Q.push(S);
dis[S] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i : e[u]) {
auto [v, f] = l[i];
if (f && dis[v] == -1) {
dis[v] = dis[u] + 1;
Q.push(v);
}
}
}
return dis[T] != -1;
}
int dfs(int u, int limit) {
if (u == T) return limit;
int fl = 0;
for (int t = cur[u]; t < e[u].size() && fl < limit; t = max(t + 1, cur[u])) {
cur[u] = t + 1;
int i = e[u][t];
auto &[v, f] = l[i];
if (f && dis[v] == dis[u] + 1) {
int g = dfs(v, min(limit - fl, f));
fl += g;
f -= g;
l[i ^ 1].f += g;
if (!f) dis[v] = -1;
}
}
return fl;
}
int flow() {
int res = 0;
while (bfs()) {
memset(cur, 0, sizeof cur);
res += dfs(S, 1e9);
}
return res;
}
} G;
int main() {
cin.tie(0)->sync_with_stdio(0);
int L, R, M;
cin >> L >> R >> M;
S = L + R + 1, T = S + 1;
for (int i = 1, u, v; i <= M; i++) {
cin >> u >> v;
u++, v++;
G.add(u, L + v, 1);
}
for (int i = 1; i <= L; i++) {
G.add(S, i, 1);
}
for (int i = 1; i <= R; i++) {
G.add(L + i, T, 1);
}
cout << G.flow() << "\n";
for (int u = 1; u <= L; u++) {
for (auto i : G.e[u]) {
auto [v, f] = G.l[i];
if (v > L && v <= L + R && !f) {
cout << u - 1 << " " << v - L - 1 << "\n";
}
}
}
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...