Submission #172979

# Submission time Handle Problem Language Result Execution time Memory
172979 2020-01-02T22:01:14 Z VEGAnn Marriage questions (IZhO14_marriage) C++14
100 / 100
892 ms 1144 KB
#include <bits/stdc++.h>
#define sz(x) ((int)x.size())
#define PB push_back
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
const int N = 2010;
const int M = 30010;
const int PW = 10;
const int oo = 2e9;
const int md = 998244353;
vector<int> g[N];
queue<int> q;
int n, m, k, mrk[M], used[N], tt = 0, lf, rt;
ll ans = 0;

bool dfs(int v){
    if (used[v] == tt) return 0;
    used[v] = tt;
    for (int u : g[v])
        if (u >= lf && u <= rt && (mrk[u] < 0 || dfs(mrk[u]))){
            mrk[u] = v;
            return 1;
        }
    return 0;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < k; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        g[y].PB(x);
    }

    fill(mrk, mrk + n, -1);

    for (int i = 0; i < m; i++)
        q.push(i);

    for (int l = 0, r = 0; l < n; l++){

        while (r < n && sz(q)) {
            lf = l; rt = r;
            while (sz(q)) {
                tt++;
                int v = q.front();
                if (dfs(v))
                    q.pop();
                else break;
            }
            if (!sz(q)) break;
            r++;
        }

        if (sz(q))
            break;

        ans += n - r;

        if (mrk[l] >= 0){
            q.push(mrk[l]);
            mrk[l] = -1;
        }
    }

    cout << ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 0 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 424 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 0 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 4 ms 484 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 42 ms 504 KB Output is correct
26 Correct 6 ms 376 KB Output is correct
27 Correct 2 ms 380 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 4 ms 376 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 307 ms 860 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 7 ms 376 KB Output is correct
35 Correct 121 ms 888 KB Output is correct
36 Correct 89 ms 892 KB Output is correct
37 Correct 892 ms 864 KB Output is correct
38 Correct 23 ms 888 KB Output is correct
39 Correct 136 ms 632 KB Output is correct
40 Correct 5 ms 504 KB Output is correct
41 Correct 7 ms 632 KB Output is correct
42 Correct 9 ms 632 KB Output is correct
43 Correct 13 ms 760 KB Output is correct
44 Correct 22 ms 1016 KB Output is correct
45 Correct 11 ms 764 KB Output is correct
46 Correct 29 ms 1144 KB Output is correct
47 Correct 26 ms 1016 KB Output is correct
48 Correct 23 ms 1016 KB Output is correct
49 Correct 34 ms 1144 KB Output is correct
50 Correct 304 ms 632 KB Output is correct