/*
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\
\\ //
// 271828___182845__904523__53602__ \\
\\ 87___47____13______52____66__24_ //
// 97___75____72______47____09___36 \\
\\ 999595_____74______96____69___67 //
// 62___77____24______07____66__30_ \\
\\ 35___35____47______59____45713__ //
// \\
\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
*/
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
#include <cstdio>
#include <functional>
using namespace std;
using LL = long long;
const int N = 1e3 + 5;
const LL mod = 998244353, inf = 1e18;
vector<int> dx = { 1, 0, -1, 0, 1, -1, 1, -1 };
vector<int> dy = { 0, -1, 0, 1, -1, 1, 1, -1 };
void solve() {
int n, m, k; cin >> n >> m >> k;
vector<vector<int>> gp(n + m);
for (int i = 0; i < k; ++i) {
int u, v; cin >> u >> v;
u += m;
gp[--u].push_back(--v);
gp[v].push_back(u);
}
int ans = 0;
vector<int> mt(n + m, -1);
vector<bool> vis(n + m);
function<bool(int, int, int)> kuhn = [&](int u, int l, int r) {
if (vis[u]) return 0;
vis[u] = 1;
for (int& v : gp[u]) {
if (v < l || v > r) continue;
if (mt[v] == -1 || kuhn(mt[v], l, r)) {
mt[v] = u;
return 1;
}
}
return 0;
};
int l = m - 2, r = n, idx = -1;
while (l + 1 < r) {
int mid = (l + r) / 2;
mt = vector<int>(n + m, -1);
int cnt = 0;
for (int i = 0; i < m; ++i) {
vis = vector<bool>(n + m);
cnt += kuhn(i, 0, m + mid);
}
if(cnt == m){
r = mid;
idx = mid;
}
else {
l = mid;
}
}
if (idx == -1) {
cout << "0\n";
return;
}
ans += n - idx;
mt = vector<int>(n + m, -1);
for (int i = 0; i < m; ++i) {
vis = vector<bool>(n + m);
kuhn(i, m, idx + m);
}
for (int i = 0; i < n; ++i) {
if (mt[i + m] == -1) {
ans += n - idx;
continue;
}
int u = mt[i + m];
while (idx < n) {
vis = vector<bool>(n + m);
if (kuhn(u, i + 1 + m, idx + m)) break;
++idx;
}
if (idx == n) break;
ans += n - idx;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t; cin >> t; while (t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
35 ms |
516 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
229 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
27 ms |
456 KB |
Output is correct |
35 |
Correct |
192 ms |
1476 KB |
Output is correct |
36 |
Correct |
151 ms |
1432 KB |
Output is correct |
37 |
Correct |
754 ms |
1192 KB |
Output is correct |
38 |
Correct |
24 ms |
1748 KB |
Output is correct |
39 |
Correct |
587 ms |
1016 KB |
Output is correct |
40 |
Correct |
6 ms |
980 KB |
Output is correct |
41 |
Correct |
12 ms |
1224 KB |
Output is correct |
42 |
Correct |
8 ms |
1364 KB |
Output is correct |
43 |
Correct |
12 ms |
1580 KB |
Output is correct |
44 |
Correct |
22 ms |
2072 KB |
Output is correct |
45 |
Correct |
17 ms |
2260 KB |
Output is correct |
46 |
Correct |
40 ms |
2976 KB |
Output is correct |
47 |
Correct |
26 ms |
2772 KB |
Output is correct |
48 |
Correct |
26 ms |
2776 KB |
Output is correct |
49 |
Correct |
55 ms |
3032 KB |
Output is correct |
50 |
Correct |
866 ms |
1784 KB |
Output is correct |