#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<unordered_set<int>> adj; // l -> r
vt<int> mate; // mate = rhs -> lhs
bool seen[2001]; // seen of lhs
bool dfs(int u) {
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;
}
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);
}
// enqueue all the unmatched girls
queue<int> to_match;
F0R (i, n) to_match.push(i);
// attempt to augment from u on lhs
auto augment = [&] (int u) {
memset(seen, 0, sizeof(seen));
return dfs(u);
};
ll ans = 0;
int r = -1;
F0R (l, m) {
while (size(to_match)) {
if (r == m - 1) {
cout << ans << endl;
return 0;
}
r++;
// add these edges
for (int u : radj[r]) adj[u].insert(r);
// try augment
while (size(to_match) && augment(to_match.front())) {
to_match.pop();
}
}
// add # of possible right endpoints for l
ans += m - r;
// remove edges
for (int u : radj[l]) adj[u].erase(l);
// check if we unmatched someone by removing l
if (mate[l] != -1) {
to_match.push(mate[l]);
mate[l] = -1;
}
}
}
Compilation message
marriage.cpp:98:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
98 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
592 KB |
Output is correct |
26 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Incorrect |
2 ms |
828 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
31 |
Correct |
57 ms |
2312 KB |
Output is correct |
32 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
4 ms |
644 KB |
Output is correct |
35 |
Correct |
15 ms |
5468 KB |
Output is correct |
36 |
Correct |
13 ms |
4956 KB |
Output is correct |
37 |
Correct |
98 ms |
2388 KB |
Output is correct |
38 |
Incorrect |
17 ms |
6220 KB |
Output isn't correct |
39 |
Correct |
86 ms |
1296 KB |
Output is correct |
40 |
Correct |
4 ms |
1628 KB |
Output is correct |
41 |
Incorrect |
6 ms |
1884 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
2908 KB |
Output isn't correct |
43 |
Incorrect |
11 ms |
3804 KB |
Output isn't correct |
44 |
Incorrect |
17 ms |
5980 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
2908 KB |
Output isn't correct |
46 |
Incorrect |
29 ms |
4380 KB |
Output isn't correct |
47 |
Incorrect |
27 ms |
7300 KB |
Output isn't correct |
48 |
Incorrect |
19 ms |
6748 KB |
Output isn't correct |
49 |
Incorrect |
31 ms |
4536 KB |
Output isn't correct |
50 |
Correct |
183 ms |
1876 KB |
Output is correct |