Submission #154489

# Submission time Handle Problem Language Result Execution time Memory
154489 2019-09-22T06:22:30 Z srvlt Marriage questions (IZhO14_marriage) C++14
48 / 100
502 ms 7488 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 2040 KB Output is correct
3 Correct 4 ms 2168 KB Output is correct
4 Incorrect 4 ms 2168 KB Output isn't correct
5 Correct 4 ms 2168 KB Output is correct
6 Correct 4 ms 2168 KB Output is correct
7 Incorrect 4 ms 2168 KB Output isn't correct
8 Correct 4 ms 2040 KB Output is correct
9 Correct 3 ms 2040 KB Output is correct
10 Correct 4 ms 2168 KB Output is correct
11 Incorrect 5 ms 2168 KB Output isn't correct
12 Correct 4 ms 2168 KB Output is correct
13 Correct 4 ms 2168 KB Output is correct
14 Incorrect 4 ms 2168 KB Output isn't correct
15 Incorrect 5 ms 2244 KB Output isn't correct
16 Incorrect 4 ms 2040 KB Output isn't correct
17 Incorrect 4 ms 2040 KB Output isn't correct
18 Incorrect 5 ms 1912 KB Output isn't correct
19 Incorrect 7 ms 2168 KB Output isn't correct
20 Incorrect 4 ms 2168 KB Output isn't correct
21 Correct 4 ms 2040 KB Output is correct
22 Incorrect 4 ms 2168 KB Output isn't correct
23 Incorrect 4 ms 2168 KB Output isn't correct
24 Incorrect 4 ms 2168 KB Output isn't correct
25 Incorrect 23 ms 2552 KB Output isn't correct
26 Incorrect 6 ms 2172 KB Output isn't correct
27 Correct 4 ms 2172 KB Output is correct
28 Incorrect 4 ms 2040 KB Output isn't correct
29 Correct 7 ms 2424 KB Output is correct
30 Correct 7 ms 2424 KB Output is correct
31 Incorrect 468 ms 4088 KB Output isn't correct
32 Incorrect 15 ms 2296 KB Output isn't correct
33 Correct 4 ms 2168 KB Output is correct
34 Incorrect 5 ms 2168 KB Output isn't correct
35 Incorrect 67 ms 4984 KB Output isn't correct
36 Incorrect 57 ms 4728 KB Output isn't correct
37 Incorrect 502 ms 4216 KB Output isn't correct
38 Correct 50 ms 5496 KB Output is correct
39 Incorrect 37 ms 2528 KB Output isn't correct
40 Correct 10 ms 2808 KB Output is correct
41 Correct 27 ms 3192 KB Output is correct
42 Correct 21 ms 3576 KB Output is correct
43 Correct 28 ms 4088 KB Output is correct
44 Correct 44 ms 5372 KB Output is correct
45 Correct 102 ms 4472 KB Output is correct
46 Incorrect 291 ms 7304 KB Output isn't correct
47 Correct 62 ms 6012 KB Output is correct
48 Correct 80 ms 5880 KB Output is correct
49 Correct 339 ms 7488 KB Output is correct
50 Incorrect 46 ms 2552 KB Output isn't correct