#include <bits/stdc++.h>
using namespace std;
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
template<typename Tuple, size_t... Is>
void print_tuple(const Tuple& t, index_sequence<Is...>) {
((cerr << (Is == 0 ? "{" : ", ") << get<Is>(t)), ...);
cerr << "}";
}
template<typename... Args>
ostream& operator<<(ostream& os, const tuple<Args...>& t) {
print_tuple(t, index_sequence_for<Args...>{});
return os;
}
ostream& operator<<(ostream& os, string& s) {
for (char c : s) os << c;
return os;
}
template<template<typename> class Container, typename T>
ostream& operator<<(ostream& os, Container<T> o) {
os << "{";
int g = size(o);
for (auto i : o) os << i << ((--g) == 0 ? "" : ", ");
os << "}";
return os;
}
template<typename T, typename V>
ostream& operator<<(ostream& os, const pair<T, V> p) {
os << "{" << p.f << ", " << p.s << "}";
return os;
}
template <typename T, typename... V>
void printer(const char *names, T &&head, V &&...tail) {
int i = 0;
while (names[i] != '\0' && names[i] != ',') i++;
constexpr bool is_str = is_same_v<decay_t<T>, const char*>;
if constexpr (is_str) cerr << head; // ignore directly passed strings
else cerr.write(names, i) << " = " << head;
if constexpr (sizeof...(tail)) cerr << (is_str ? "" : ","), printer(names + i + 1, tail...);
else cerr << endl;
}
#ifdef LOCAL
#define dbg(...) std::cerr << __LINE__ << ": ", printer(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(x...)
#define cerr if (0) std::cerr
#endif
/*
range perfect matching goes crazy
since kuhn's is more efficient when lhs is small, we need the girls to be on the left
we want to find the maximum r for each l, and two pointer it
*/
int n, m, k; // n is lhs (girls), m is rhs (boys)
vt<vt<int>> adj; // l -> r
vt<int> mate; // mate = rhs -> lhs
vt<bool> seen;
bool dfs(int u) {
assert(u != -1);
if (seen[u]) return false;
seen[u] = true;
for (int v : adj[u]) {
if (mate[v] == -1 || dfs(mate[v])) {
mate[v] = u;
return true;
}
}
return false;
}
bool check_perfect_matching() {
seen.assign(n, 0);
mate.assign(m, -1);
F0R (i, n) {
if (!dfs(i)) return false;
}
return true;
}
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> m >> n >> k;
adj.resize(n);
mate.resize(m, -1);
vt<vt<int>> radj(m);
F0R (i, k) {
int u, v; cin >> u >> v; u--, v--;
// u is boy, v is girl
radj[u].pb(v);
}
ll ans = 0;
F0R (l, m) {
FOR (r, l, m) {
adj.assign(n, {});
FOR (i, l, r + 1) {
for (int u : radj[i]) {
adj[u].pb(i);
}
}
if (check_perfect_matching()) ans++;
}
}
cout << ans << endl;
}
Compilation message
marriage.cpp:108:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
108 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
19 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
4 ms |
344 KB |
Output isn't correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Incorrect |
4 ms |
348 KB |
Output isn't correct |
23 |
Correct |
10 ms |
348 KB |
Output is correct |
24 |
Correct |
8 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1360 ms |
624 KB |
Output isn't correct |
26 |
Incorrect |
374 ms |
500 KB |
Output isn't correct |
27 |
Correct |
162 ms |
348 KB |
Output is correct |
28 |
Incorrect |
188 ms |
452 KB |
Output isn't correct |
29 |
Correct |
1331 ms |
344 KB |
Output is correct |
30 |
Correct |
1134 ms |
344 KB |
Output is correct |
31 |
Execution timed out |
1555 ms |
860 KB |
Time limit exceeded |
32 |
Execution timed out |
1527 ms |
344 KB |
Time limit exceeded |
33 |
Correct |
1254 ms |
488 KB |
Output is correct |
34 |
Execution timed out |
1515 ms |
344 KB |
Time limit exceeded |
35 |
Execution timed out |
1551 ms |
1368 KB |
Time limit exceeded |
36 |
Execution timed out |
1520 ms |
1328 KB |
Time limit exceeded |
37 |
Execution timed out |
1586 ms |
1084 KB |
Time limit exceeded |
38 |
Execution timed out |
1534 ms |
1772 KB |
Time limit exceeded |
39 |
Execution timed out |
1575 ms |
844 KB |
Time limit exceeded |
40 |
Execution timed out |
1572 ms |
860 KB |
Time limit exceeded |
41 |
Execution timed out |
1595 ms |
1424 KB |
Time limit exceeded |
42 |
Execution timed out |
1541 ms |
1336 KB |
Time limit exceeded |
43 |
Execution timed out |
1546 ms |
1668 KB |
Time limit exceeded |
44 |
Execution timed out |
1531 ms |
1872 KB |
Time limit exceeded |
45 |
Execution timed out |
1557 ms |
1952 KB |
Time limit exceeded |
46 |
Execution timed out |
1565 ms |
2608 KB |
Time limit exceeded |
47 |
Execution timed out |
1575 ms |
2384 KB |
Time limit exceeded |
48 |
Execution timed out |
1513 ms |
2388 KB |
Time limit exceeded |
49 |
Execution timed out |
1530 ms |
2736 KB |
Time limit exceeded |
50 |
Execution timed out |
1569 ms |
1372 KB |
Time limit exceeded |