Submission #154487

# Submission time Handle Problem Language Result Execution time Memory
154487 2019-09-22T06:19:44 Z srvlt Marriage questions (IZhO14_marriage) C++14
48 / 100
486 ms 7596 KB
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse2,avx")
#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
//#define int long long
using namespace std;

void dout() {
    cerr << endl;
}

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 32007;
int n, m, k, mt[N], used[N], f[N], timer;
vector <int> q[N], w[N];
bool rem[N];

bool dfs(int v) {
    if (used[v] == timer) {
        return false;
    }
    used[v] = timer;
    for (auto i : q[v]) {
        if (rem[i]) {
            continue;
        }
        if (mt[i] == -1) {
            mt[i] = v;
            f[v] = i;
            return true;
        }
    }
    for (auto i : q[v]) {
        if (rem[i]) {
            continue;
        }
        if (dfs(mt[i])) {
            mt[i] = v;
            f[v] = i;
            return true;
        }
    }
    return false;
}

bool ok() {
    for (int i = 1; i <= m; i++) {
        if (f[i + n] == -1) {
            return false;
        }
    }
    return true;
}

void del(int x) {
    int tmp = mt[x];
    if (tmp != -1) {
        f[tmp] = -1;
        mt[x] = -1;
    }
    rem[x] = true;
    timer++;
    dfs(tmp);
    q[x] = {};
}

void add(int x) {
    for (auto i : w[x]) {
        q[x].pb(i);
        q[i].pb(x);
    }
    for (auto i : q[x]) {
        if (f[i] == -1) {
            timer++;
            dfs(i);
        }
    }
}

void solve(int tc) {
    for (int i = 0; i < N; i++) {
        f[i] = mt[i] = -1;
    }
    cin >> n >> m >> k;
    for (int i = 1; i <= k; i++) {
        int x, y;
        cin >> x >> y;
        w[x].pb(y + n);
        w[y + n].pb(x);
    }
    int j = 1, res = 0;
    for (int i = 1; i <= n; i++) {
        while (j <= n) {
            add(j);
            if (ok()) {
                break;
            }
            j++;
        }
        res += (n - j + 1);
        del(i);
    }
    cout << res;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int tc = 1;
//    cin >> tc;
    for (int i = 0; i < tc; i++) {
        solve(i);
//        cleanup();
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2168 KB Output isn't correct
2 Correct 4 ms 2168 KB Output is correct
3 Correct 4 ms 2168 KB Output is correct
4 Incorrect 4 ms 2040 KB Output isn't correct
5 Correct 4 ms 2168 KB Output is correct
6 Correct 4 ms 2040 KB Output is correct
7 Incorrect 4 ms 2040 KB Output isn't correct
8 Correct 4 ms 2168 KB Output is correct
9 Correct 4 ms 2040 KB Output is correct
10 Correct 4 ms 2040 KB Output is correct
11 Incorrect 4 ms 2040 KB Output isn't correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 5 ms 2040 KB Output is correct
14 Incorrect 4 ms 2168 KB Output isn't correct
15 Incorrect 4 ms 2168 KB Output isn't correct
16 Incorrect 5 ms 2040 KB Output isn't correct
17 Incorrect 4 ms 2040 KB Output isn't correct
18 Incorrect 4 ms 2040 KB Output isn't correct
19 Incorrect 7 ms 2168 KB Output isn't correct
20 Incorrect 5 ms 2168 KB Output isn't correct
21 Correct 5 ms 2168 KB Output is correct
22 Incorrect 21 ms 2040 KB Output isn't correct
23 Incorrect 4 ms 2168 KB Output isn't correct
24 Incorrect 5 ms 2168 KB Output isn't correct
25 Incorrect 22 ms 2552 KB Output isn't correct
26 Incorrect 6 ms 2296 KB Output isn't correct
27 Correct 5 ms 2168 KB Output is correct
28 Incorrect 3 ms 2168 KB Output isn't correct
29 Correct 7 ms 2424 KB Output is correct
30 Correct 6 ms 2424 KB Output is correct
31 Incorrect 461 ms 4176 KB Output isn't correct
32 Incorrect 14 ms 2296 KB Output isn't correct
33 Correct 4 ms 2296 KB Output is correct
34 Incorrect 6 ms 2168 KB Output isn't correct
35 Incorrect 66 ms 5072 KB Output isn't correct
36 Incorrect 54 ms 4984 KB Output isn't correct
37 Incorrect 486 ms 4152 KB Output isn't correct
38 Correct 47 ms 5644 KB Output is correct
39 Incorrect 31 ms 2552 KB Output isn't correct
40 Correct 10 ms 2808 KB Output is correct
41 Correct 28 ms 3232 KB Output is correct
42 Correct 21 ms 3576 KB Output is correct
43 Correct 30 ms 3960 KB Output is correct
44 Correct 45 ms 5368 KB Output is correct
45 Correct 105 ms 4572 KB Output is correct
46 Incorrect 277 ms 7160 KB Output isn't correct
47 Correct 62 ms 6136 KB Output is correct
48 Correct 63 ms 5832 KB Output is correct
49 Correct 334 ms 7596 KB Output is correct
50 Incorrect 46 ms 2552 KB Output isn't correct