//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2007;
int n, m, k, mt[N], used[N], f[N], timer;
vector <int> q[N], w[N];
bool dfs(int v) {
if (used[v] == timer) {
return false;
}
used[v] = timer;
for (auto i : q[v]) {
if (mt[i] == -1) {
mt[i] = v;
f[v] = i;
return true;
}
}
for (auto i : q[v]) {
if (dfs(mt[i])) {
mt[i] = v;
f[v] = i;
return true;
}
}
return false;
}
bool kuhn() {
for (int i = 0; i < N; i++) {
timer = 0;
used[i] = 0;
mt[i] = f[i] = -1;
}
for (int i = 1; i <= m; i++) {
timer++;
if (f[i + n] == -1) {
dfs(i + n);
}
}
for (int i = 1; i <= m; i++) {
if (f[i + n] == -1) {
return false;
}
}
}
void del(int x) {
for (auto i : q[x]) {
for (int j = 0; j < q[i].size(); j++) {
if (q[i][j] == x) {
q[i].erase(q[i].begin() + j);
break;
}
}
}
q[x] = {};
}
void add(int x) {
for (auto i : w[x]) {
q[x].pb(i);
q[i].pb(x);
}
}
void solve(int tc) {
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
w[x].pb(y + n);
w[y + n].pb(x);
}
int j = 1, res = 0;
for (int i = 1; i <= n; i++) {
while (j <= n) {
add(j);
if (kuhn()) {
break;
}
j++;
}
res += (n - j + 1);
del(i);
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
Compilation message
marriage.cpp: In function 'void del(int)':
marriage.cpp:76:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < q[i].size(); j++) {
~~^~~~~~~~~~~~~
marriage.cpp: In function 'bool kuhn()':
marriage.cpp:72:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Incorrect |
3 ms |
376 KB |
Output isn't correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
4 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
3 ms |
476 KB |
Output is correct |
23 |
Correct |
3 ms |
504 KB |
Output is correct |
24 |
Correct |
3 ms |
504 KB |
Output is correct |
25 |
Correct |
39 ms |
936 KB |
Output is correct |
26 |
Correct |
14 ms |
632 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
7 ms |
508 KB |
Output is correct |
29 |
Correct |
8 ms |
760 KB |
Output is correct |
30 |
Correct |
8 ms |
760 KB |
Output is correct |
31 |
Execution timed out |
1567 ms |
1752 KB |
Time limit exceeded |
32 |
Correct |
306 ms |
760 KB |
Output is correct |
33 |
Correct |
12 ms |
504 KB |
Output is correct |
34 |
Correct |
23 ms |
504 KB |
Output is correct |
35 |
Correct |
200 ms |
3704 KB |
Output is correct |
36 |
Correct |
155 ms |
3448 KB |
Output is correct |
37 |
Runtime error |
17 ms |
2424 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
38 |
Runtime error |
39 ms |
4216 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
39 |
Runtime error |
3 ms |
760 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
40 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
41 |
Runtime error |
3 ms |
760 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
42 |
Runtime error |
3 ms |
760 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
43 |
Runtime error |
4 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
44 |
Runtime error |
4 ms |
784 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
45 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
46 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
47 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
48 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
49 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
50 |
Runtime error |
3 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |