Submission #594040

# Submission time Handle Problem Language Result Execution time Memory
594040 2022-07-12T00:35:33 Z skittles1412 Parachute rings (IOI12_rings) C++17
100 / 100
1194 ms 95788 KB
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

const int maxn = 1e6 + 5;

struct DSU {
    int cyc, cu, p[maxn];

    DSU() : cyc(false) {
        memset(p, -1, sizeof(p));
    }

    int find(int u) {
        return p[u] < 0 ? u : (p[u] = find(p[u]));
    }

    void merge(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) {
            cu = u;
            cyc++;
            return;
        }
        if (p[u] < p[v]) {
            swap(u, v);
        }
        p[v] += p[u];
        p[u] = v;
    }
} dsu, dsus[4];

int n, zans = -1, dcnt[5], big[maxn], imp[4];
vector<int> graph[maxn];
vector<pair<int, int>> edges;

void Init(int _n) {
    n = _n;
    imp[0] = -1;
}

void upd_dcnt(int d) {
    dcnt[min(d, 4)]--;
    dcnt[min(d + 1, 4)]++;
}

void Link(int u, int v) {
    dsu.merge(u, v);
    edges.emplace_back(u, v);
    upd_dcnt(sz(graph[u]));
    upd_dcnt(sz(graph[v]));
    if (sz(graph[u]) >= 3) {
        big[v]++;
    }
    if (sz(graph[v]) >= 3) {
        big[u]++;
    }
    graph[u].push_back(v);
    graph[v].push_back(u);
    if (sz(graph[u]) == 3) {
        big[u]++;
        for (auto& a : graph[u]) {
            big[a]++;
        }
    }
    if (sz(graph[v]) == 3) {
        big[v]++;
        for (auto& a : graph[v]) {
            big[a]++;
        }
    }
    if (imp[0] == -1) {
        int cx = -1;
        if (sz(graph[u]) >= 3) {
            cx = u;
        } else if (sz(graph[v]) >= 3) {
            cx = v;
        }
        if (cx != -1) {
            assert(sz(graph[cx]) == 3);
            for (int i = 0; i < 4; i++) {
                int cy;
                if (i < 3) {
                    cy = graph[cx][i];
                } else {
                    cy = cx;
                }
                imp[i] = cy;
                for (auto& [u, v] : edges) {
                    if (u != cy && v != cy) {
                        dsus[i].merge(u, v);
                    }
                }
            }
        }
    } else {
        for (int i = 0; i < 4; i++) {
            if (u != imp[i] && v != imp[i]) {
                dsus[i].merge(u, v);
            }
        }
    }
}

int CountCritical() {
    if (dcnt[4] >= 2) {
        return 0;
    } else if (imp[0] != -1) {
        int ans = 0;
        for (int i = 0; i < 4; i++) {
            bool cur = !dsus[i].cyc;
            if (dcnt[4] == 1) {
                cur &= sz(graph[imp[i]]) >= 4;
            }
            cur &= big[imp[i]] == dcnt[3] + dcnt[4];
            ans += cur;
        }
        return ans;
    } else {
        if (dsu.cyc > 1) {
            return 0;
        } else if (dsu.cyc == 1) {
            if (zans != -1) {
                return zans;
            }
            zans = 0;
            int u = dsu.cu, p = -1;
            dbg(u);
            do {
                assert(sz(graph[u]) == 2);
                if (p != graph[u][0]) {
                    p = u;
                    u = graph[u][0];
                } else {
                    p = u;
                    u = graph[u][1];
                }
                dbg(u);
                zans++;
            } while (u != dsu.cu);
            return zans;
        }
        assert(!dcnt[4] && !dcnt[3]);
        return n;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43348 KB Output is correct
2 Correct 20 ms 43480 KB Output is correct
3 Correct 20 ms 43612 KB Output is correct
4 Correct 20 ms 43348 KB Output is correct
5 Correct 19 ms 43412 KB Output is correct
6 Correct 20 ms 43568 KB Output is correct
7 Correct 19 ms 43348 KB Output is correct
8 Correct 21 ms 43604 KB Output is correct
9 Correct 20 ms 43604 KB Output is correct
10 Correct 20 ms 43584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 298 ms 63628 KB Output is correct
2 Correct 932 ms 74684 KB Output is correct
3 Correct 1194 ms 81872 KB Output is correct
4 Correct 762 ms 82508 KB Output is correct
5 Correct 777 ms 95788 KB Output is correct
6 Correct 862 ms 94112 KB Output is correct
7 Correct 1149 ms 93240 KB Output is correct
8 Correct 1004 ms 92584 KB Output is correct
9 Correct 986 ms 95780 KB Output is correct
10 Correct 532 ms 92892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43348 KB Output is correct
2 Correct 20 ms 43480 KB Output is correct
3 Correct 20 ms 43612 KB Output is correct
4 Correct 20 ms 43348 KB Output is correct
5 Correct 19 ms 43412 KB Output is correct
6 Correct 20 ms 43568 KB Output is correct
7 Correct 19 ms 43348 KB Output is correct
8 Correct 21 ms 43604 KB Output is correct
9 Correct 20 ms 43604 KB Output is correct
10 Correct 20 ms 43584 KB Output is correct
11 Correct 22 ms 43604 KB Output is correct
12 Correct 23 ms 43856 KB Output is correct
13 Correct 27 ms 43860 KB Output is correct
14 Correct 22 ms 43732 KB Output is correct
15 Correct 22 ms 43648 KB Output is correct
16 Correct 23 ms 43860 KB Output is correct
17 Correct 23 ms 43860 KB Output is correct
18 Correct 27 ms 44144 KB Output is correct
19 Correct 23 ms 43868 KB Output is correct
20 Correct 24 ms 43980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43348 KB Output is correct
2 Correct 20 ms 43480 KB Output is correct
3 Correct 20 ms 43612 KB Output is correct
4 Correct 20 ms 43348 KB Output is correct
5 Correct 19 ms 43412 KB Output is correct
6 Correct 20 ms 43568 KB Output is correct
7 Correct 19 ms 43348 KB Output is correct
8 Correct 21 ms 43604 KB Output is correct
9 Correct 20 ms 43604 KB Output is correct
10 Correct 20 ms 43584 KB Output is correct
11 Correct 22 ms 43604 KB Output is correct
12 Correct 23 ms 43856 KB Output is correct
13 Correct 27 ms 43860 KB Output is correct
14 Correct 22 ms 43732 KB Output is correct
15 Correct 22 ms 43648 KB Output is correct
16 Correct 23 ms 43860 KB Output is correct
17 Correct 23 ms 43860 KB Output is correct
18 Correct 27 ms 44144 KB Output is correct
19 Correct 23 ms 43868 KB Output is correct
20 Correct 24 ms 43980 KB Output is correct
21 Correct 33 ms 44920 KB Output is correct
22 Correct 43 ms 45836 KB Output is correct
23 Correct 48 ms 46584 KB Output is correct
24 Correct 73 ms 46404 KB Output is correct
25 Correct 35 ms 44316 KB Output is correct
26 Correct 53 ms 47108 KB Output is correct
27 Correct 55 ms 47708 KB Output is correct
28 Correct 58 ms 48192 KB Output is correct
29 Correct 51 ms 46176 KB Output is correct
30 Correct 60 ms 48376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 43348 KB Output is correct
2 Correct 20 ms 43480 KB Output is correct
3 Correct 20 ms 43612 KB Output is correct
4 Correct 20 ms 43348 KB Output is correct
5 Correct 19 ms 43412 KB Output is correct
6 Correct 20 ms 43568 KB Output is correct
7 Correct 19 ms 43348 KB Output is correct
8 Correct 21 ms 43604 KB Output is correct
9 Correct 20 ms 43604 KB Output is correct
10 Correct 20 ms 43584 KB Output is correct
11 Correct 298 ms 63628 KB Output is correct
12 Correct 932 ms 74684 KB Output is correct
13 Correct 1194 ms 81872 KB Output is correct
14 Correct 762 ms 82508 KB Output is correct
15 Correct 777 ms 95788 KB Output is correct
16 Correct 862 ms 94112 KB Output is correct
17 Correct 1149 ms 93240 KB Output is correct
18 Correct 1004 ms 92584 KB Output is correct
19 Correct 986 ms 95780 KB Output is correct
20 Correct 532 ms 92892 KB Output is correct
21 Correct 22 ms 43604 KB Output is correct
22 Correct 23 ms 43856 KB Output is correct
23 Correct 27 ms 43860 KB Output is correct
24 Correct 22 ms 43732 KB Output is correct
25 Correct 22 ms 43648 KB Output is correct
26 Correct 23 ms 43860 KB Output is correct
27 Correct 23 ms 43860 KB Output is correct
28 Correct 27 ms 44144 KB Output is correct
29 Correct 23 ms 43868 KB Output is correct
30 Correct 24 ms 43980 KB Output is correct
31 Correct 33 ms 44920 KB Output is correct
32 Correct 43 ms 45836 KB Output is correct
33 Correct 48 ms 46584 KB Output is correct
34 Correct 73 ms 46404 KB Output is correct
35 Correct 35 ms 44316 KB Output is correct
36 Correct 53 ms 47108 KB Output is correct
37 Correct 55 ms 47708 KB Output is correct
38 Correct 58 ms 48192 KB Output is correct
39 Correct 51 ms 46176 KB Output is correct
40 Correct 60 ms 48376 KB Output is correct
41 Correct 179 ms 56976 KB Output is correct
42 Correct 422 ms 69568 KB Output is correct
43 Correct 255 ms 54464 KB Output is correct
44 Correct 680 ms 93996 KB Output is correct
45 Correct 795 ms 82856 KB Output is correct
46 Correct 518 ms 87888 KB Output is correct
47 Correct 744 ms 88612 KB Output is correct
48 Correct 471 ms 72724 KB Output is correct
49 Correct 519 ms 84888 KB Output is correct
50 Correct 540 ms 84480 KB Output is correct
51 Correct 278 ms 56344 KB Output is correct
52 Correct 628 ms 85172 KB Output is correct
53 Correct 475 ms 73168 KB Output is correct
54 Correct 926 ms 86928 KB Output is correct
55 Correct 1120 ms 91528 KB Output is correct