답안 #700949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
700949 2023-02-19T13:12:28 Z KiriLL1ca 결혼 문제 (IZhO14_marriage) C++17
100 / 100
1233 ms 1832 KB
#include <bits/stdc++.h>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define endl '\n'
#define sz(x) (int)((x).size())
#define vec vector
#define pw(x) (1ll << x)

using namespace std;

template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;

const int N = 3e4 + 2e3 + 10;
vec <int> g[N];
int used[N], mt[N], us = 1;

inline bool ok (int u, int l, int r) { return l <= u && u <= r; }

inline bool kuhn (int v, int l, int r) {
    if (used[v] == us) return 0;
    used[v] = us;
    for (auto &u : g[v]) if (ok(u, l, r) && !~mt[u]) { mt[u] = v; return 1; }
    for (auto &u : g[v]) if (ok(u, l, r) && kuhn(mt[u], l, r)) { mt[u] = v; return 1; }
    return 0;
}

inline void solve () {
    int n, m, k; cin >> n >> m >> k;
    for (int i = 0; i < k; ++i) {
        int u, v; cin >> u >> v; --u, --v;
        g[v].pb(u);
    }

    fill(mt, mt + N, -1);
    vec <int> girls;
    for (int i = 0; i < m; ++i) girls.pb(i);

    ll ans = 0;
    for (int l = 0, r = 0; l < n; ++l) {
        while (r < n) {
            while (sz(girls)) {
                ++us;
                int go = girls.back();
                if (kuhn(go, l, r)) girls.pop_back();
                else break;
            }
            if (!sz(girls)) break;
            ++r;
        }

        if (sz(girls)) break;

        ans += n - r;

        if (~mt[l]) {
            girls.pb(mt[l]);
            mt[l] = -1;
        }
    }
    cout << ans << endl;
}

signed main () {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif// LOCAL
    int t = 1; //cin >> t;
    while (t--) solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 1 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 1 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 1108 KB Output is correct
10 Correct 1 ms 1108 KB Output is correct
11 Correct 1 ms 1108 KB Output is correct
12 Correct 1 ms 1108 KB Output is correct
13 Correct 1 ms 1108 KB Output is correct
14 Correct 1 ms 1108 KB Output is correct
15 Correct 1 ms 1108 KB Output is correct
16 Correct 1 ms 1108 KB Output is correct
17 Correct 1 ms 1108 KB Output is correct
18 Correct 1 ms 1108 KB Output is correct
19 Correct 3 ms 1108 KB Output is correct
20 Correct 1 ms 1108 KB Output is correct
21 Correct 1 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 1 ms 1108 KB Output is correct
24 Correct 1 ms 1108 KB Output is correct
25 Correct 62 ms 1280 KB Output is correct
26 Correct 6 ms 1228 KB Output is correct
27 Correct 1 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 2 ms 1236 KB Output is correct
30 Correct 2 ms 1216 KB Output is correct
31 Correct 436 ms 1496 KB Output is correct
32 Correct 1 ms 1236 KB Output is correct
33 Correct 1 ms 1196 KB Output is correct
34 Correct 2 ms 1108 KB Output is correct
35 Correct 107 ms 1704 KB Output is correct
36 Correct 76 ms 1620 KB Output is correct
37 Correct 1233 ms 1644 KB Output is correct
38 Correct 14 ms 1620 KB Output is correct
39 Correct 48 ms 1236 KB Output is correct
40 Correct 3 ms 1236 KB Output is correct
41 Correct 6 ms 1364 KB Output is correct
42 Correct 9 ms 1236 KB Output is correct
43 Correct 10 ms 1460 KB Output is correct
44 Correct 15 ms 1748 KB Output is correct
45 Correct 7 ms 1364 KB Output is correct
46 Correct 20 ms 1832 KB Output is correct
47 Correct 18 ms 1748 KB Output is correct
48 Correct 20 ms 1628 KB Output is correct
49 Correct 21 ms 1820 KB Output is correct
50 Correct 155 ms 1292 KB Output is correct