Submission #331918

# Submission time Handle Problem Language Result Execution time Memory
331918 2020-11-30T17:38:02 Z vitkishloh228 Marriage questions (IZhO14_marriage) C++14
100 / 100
952 ms 2156 KB
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> mt;
vector<vector<int>> g;
vector<int> mt1;
int L = 0, R = 0;
vector<bool> used;
bool dfs(int v) {
    if (used[v]) return false;
    used[v] = true;
    for (auto u : g[v]) {
        if (L <= u && u <= R) {
            if (mt[u] == -1 || dfs(mt[u])) {
                mt[u] = v;
                mt1[v] = u;
                return true;
            }
        }
    }
    return false;
}
int main() {
    int m, n, k;
    cin >> m >> n >> k;
    g.resize(n);
    while (k--) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[v].push_back(u);
    }
    used.resize(n);
    mt = vector<int>(m, -1);
    mt1 = vector<int>(n, -1);
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        q.push(i);
    }
    long long ans = 0;
    for (L = 0, R = -1; max(R, L) < m;) {
        while (max(R, L) < m && !q.empty()) {
            while (!q.empty()) {
                int v = q.front();
                used = vector<bool>(n, false);
                if (dfs(v)) {
                    q.pop();
                }
                else break;
            }
            if (q.empty()) break;
            R++;
        }

        ans += m - R;
        int v0 = mt[L];
        if (v0 != -1) {
            mt[L] = -1;
            q.push(v0);
        }
        L++;
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 3 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 364 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 44 ms 492 KB Output is correct
26 Correct 5 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 5 ms 492 KB Output is correct
30 Correct 4 ms 364 KB Output is correct
31 Correct 358 ms 1024 KB Output is correct
32 Correct 5 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 6 ms 364 KB Output is correct
35 Correct 194 ms 1644 KB Output is correct
36 Correct 147 ms 1536 KB Output is correct
37 Correct 952 ms 1408 KB Output is correct
38 Correct 51 ms 1772 KB Output is correct
39 Correct 132 ms 620 KB Output is correct
40 Correct 7 ms 620 KB Output is correct
41 Correct 12 ms 748 KB Output is correct
42 Correct 17 ms 888 KB Output is correct
43 Correct 28 ms 1172 KB Output is correct
44 Correct 50 ms 1772 KB Output is correct
45 Correct 20 ms 1132 KB Output is correct
46 Correct 57 ms 2156 KB Output is correct
47 Correct 58 ms 2028 KB Output is correct
48 Correct 54 ms 1900 KB Output is correct
49 Correct 63 ms 2156 KB Output is correct
50 Correct 282 ms 876 KB Output is correct