답안 #154490

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
154490 2019-09-22T06:26:38 Z srvlt 결혼 문제 (IZhO14_marriage) C++14
90 / 100
1500 ms 7476 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 kuhn() {
    for (int i = 0; i <= n + m; i++) {
        timer = 0;
        used[i] = 0;
        mt[i] = f[i] = -1;
    }
    for (int i = 1; i <= m; i++) {
        timer++;
        if (f[i + n] == -1) {
            dfs(i + n);
        }
    }
    for (int i = 1; i <= m; i++) {
        if (f[i + n] == -1) {
            return false;
        }
    }
    return true;
}

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

void del2(int x) {
    rem[x] = true;
    q[x] = {};
}

void add2(int x) {
    for (auto i : w[x]) {
        q[x].pb(i);
        q[i].pb(x);
    }
}

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 solve2() {
    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 (kuhn()) {
                break;
            }
            j++;
        }
        res += (n - j + 1);
        del(i);
    }
    cout << res;
}

void solve() {
    for (int i = 0; i < N; i++) {
        f[i] = mt[i] = -1;
    }
    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);
    cin >> n >> m >> k;
    if (n <= 1000 && m <= 500) {
        solve2();
    }   else {
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1784 KB Output is correct
2 Correct 4 ms 1784 KB Output is correct
3 Correct 4 ms 1784 KB Output is correct
4 Correct 4 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1916 KB Output is correct
8 Correct 4 ms 1784 KB Output is correct
9 Correct 4 ms 1912 KB Output is correct
10 Correct 5 ms 1912 KB Output is correct
11 Correct 4 ms 1784 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1784 KB Output is correct
14 Correct 4 ms 1884 KB Output is correct
15 Correct 4 ms 1912 KB Output is correct
16 Correct 4 ms 1912 KB Output is correct
17 Correct 4 ms 1916 KB Output is correct
18 Correct 3 ms 1788 KB Output is correct
19 Correct 10 ms 1912 KB Output is correct
20 Correct 5 ms 1912 KB Output is correct
21 Correct 4 ms 1912 KB Output is correct
22 Correct 4 ms 1912 KB Output is correct
23 Correct 5 ms 1912 KB Output is correct
24 Correct 4 ms 1912 KB Output is correct
25 Correct 84 ms 2424 KB Output is correct
26 Correct 21 ms 2040 KB Output is correct
27 Correct 5 ms 1912 KB Output is correct
28 Correct 6 ms 1912 KB Output is correct
29 Correct 8 ms 2168 KB Output is correct
30 Correct 8 ms 2172 KB Output is correct
31 Execution timed out 1570 ms 3200 KB Time limit exceeded
32 Correct 350 ms 2168 KB Output is correct
33 Correct 12 ms 1912 KB Output is correct
34 Correct 23 ms 2040 KB Output is correct
35 Correct 335 ms 5112 KB Output is correct
36 Correct 256 ms 4936 KB Output is correct
37 Incorrect 485 ms 4248 KB Output isn't correct
38 Correct 47 ms 5496 KB Output is correct
39 Incorrect 32 ms 2680 KB Output isn't correct
40 Correct 10 ms 2812 KB Output is correct
41 Correct 28 ms 3320 KB Output is correct
42 Correct 21 ms 3576 KB Output is correct
43 Correct 27 ms 4088 KB Output is correct
44 Correct 44 ms 5368 KB Output is correct
45 Correct 104 ms 4400 KB Output is correct
46 Incorrect 276 ms 7200 KB Output isn't correct
47 Correct 62 ms 6136 KB Output is correct
48 Correct 59 ms 5952 KB Output is correct
49 Correct 316 ms 7476 KB Output is correct
50 Incorrect 46 ms 2552 KB Output isn't correct