QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#340892 | #898. 二分图最大匹配 | orz_z# | TL | 0ms | 0kb | C++14 | 2.0kb | 2024-02-29 13:51:26 | 2024-02-29 13:51:26 |
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F(i, l, r) for(int i = (l); i <= (r); ++i)
#define dF(i, r, l) for(int i = (r); i >= (l); --i)
int ri() {
int x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = x* 10 + c - 48;
c = getchar();
} return x * f;
}
const int _ = 3e5 + 5;
int n, m;
struct MCMF {
int s, t, tot = 1, head[_], to[_ << 1], nxt[_ << 1], cur[_], fl[_ << 1];
int dis[_];
void add(int u, int v, int w) {
to[++tot] = v, nxt[tot] = head[u], head[u] = tot, fl[tot] = w;
}
void Add(int u, int v, int w) {
add(u, v, w), add(v, u, 0);
}
bool bfs() {
F(i, 0, n + 2) dis[i] = -1;
dis[s] = 0;
queue<int > q;
q.push(s);
memcpy(cur, head, sizeof (cur));
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u], v = to[i]; i; i = nxt[i], v = to[i]) {
if(fl[i] > 0 && dis[v] == -1) {
dis[v] = dis[u] + 1;
q.push(v);
}
}
}
return dis[t] != -1;
}
bool lv[_];
int dfs(int u, int flow = 0x3f3f3f3f) {
if(u == t) return flow;
int rmn = flow;
lv[u] = 1;
for(int &i = cur[u], v = to[i]; i && rmn; i = nxt[i], v = to[i]) {
if(fl[i] > 0 && !lv[v] &&dis[v] == dis[u] + 1) {
int c = dfs(v, min(rmn, fl[i]));
fl[i] -= c, fl[i ^ 1] += c;
rmn -= c;
}
}
lv[u] = 0;
return flow - rmn;
}
int dinic() {
int res = 0;
while(bfs()) {
res += dfs(s);
} return res;
}
} S;
int Id[_];
pair<int, int> p[_];
signed main() {
int L = ri(), R = ri();
m = ri();
n = L + R + 5;
S.s = L + R + 1, S.t = S.s + 1;
F(i, 1, L) S.Add(S.s, i, 1);
F(i, 1, R) S.Add(i + L, S.t, 1);
F(i, 1, m) {
int u = ri() + 1, v = ri() + 1;
p[i] = {u, v};
Id[i] = S.tot + 1;
S.Add(u, v + L, 1);
}
cout << S.dinic() <<'\n';
F(i, 1, m) if(S.fl[Id[i]] == 0) {
cout << p[i].first - 1<< ' ' << p[i].second - 1<<'\n';
}
}
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...