답안 #1030615

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030615 2024-07-22T07:38:10 Z shiomusubi496 Simurgh (IOI17_simurgh) C++17
100 / 100
477 ms 23996 KB
#include "simurgh.h"

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)

#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)

using namespace std;

using ll = long long;

constexpr ll inf = 1e18;

template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }

class UnionFind {
    int n;
    vector<int> par, wei; // a[x] = a[r] + wei[x]

public:
    UnionFind(int n_) : n(n_), par(n_, -1), wei(n, 0) {}
    int find(int x) {
        if (par[x] < 0) return x;
        int r = find(par[x]);
        wei[x] += wei[par[x]];
        return par[x] = r;
    }
    int weight(int x) {
        find(x);
        return wei[x];
    }
    // a[x] = a[y] + w
    bool merge(int x, int y, int w) {
        w = weight(x) - w - weight(y);
        x = find(x), y = find(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y), w = -w;
        par[x] += par[y];
        par[y] = x;
        wei[y] = w;
        return true;
    }
    int diff(int x, int y) { return weight(y) - weight(x); }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) { return -par[find(x)]; }
};

struct edge {
    int to, idx;
    bool operator==(const edge& e) const { return to == e.to && idx == e.idx; }
};

class BiConnectedComponents {
    vector<vector<edge>> g;
    vector<int> ord, low;
    vector<int> stk;
    vector<vector<int>> bcc;
    int idx;

    void dfs(int v, int p) {
        ord[v] = low[v] = idx++;
        for (auto e : g[v]) {
            if (e.to == p) continue;
            if (ord[e.to] == -1) {
                stk.push_back(e.idx);
                dfs(e.to, v);
                chmin(low[v], low[e.to]);
                if (ord[v] <= low[e.to]) {
                    vector<int> comp;
                    while (true) {
                        int f = stk.back();
                        stk.pop_back();
                        comp.push_back(f);
                        if (f == e.idx) break;
                    }
                    bcc.push_back(comp);
                }
            } else {
                chmin(low[v], ord[e.to]);
                if (ord[v] > ord[e.to]) stk.push_back(e.idx);
            }
        }
    }

public:
    BiConnectedComponents(int n) : g(n), ord(n, -1), low(n), idx(0) {}

    void add_edge(int u, int v) {
        g[u].push_back({v, idx});
        g[v].push_back({u, idx});
        ++idx;
    }

    vector<vector<int>> build() {
        idx = 0;
        rep (i, g.size()) if (ord[i] == -1) dfs(i, -1);
        return bcc;
    }
};

map<vector<int>, int> memo;

int Query(vector<int> es) {
    sort(all(es));
    if (memo.count(es)) return memo[es];
    return memo[es] = count_common_roads(es);
}

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
    if (n == 2) return {0};
    BiConnectedComponents bcc(n);
    rep (i, u.size()) bcc.add_edge(u[i], v[i]);
    auto comp = bcc.build();
    vector<int> cmpid(u.size(), -1);
    rep (i, comp.size()) for (auto e : comp[i]) cmpid[e] = i;
    vector<vector<edge>> g1(n);
    rep (i, u.size()) {
        g1[u[i]].push_back({v[i], i});
        g1[v[i]].push_back({u[i], i});
    }
    vector<int> ans;
    rep (c, comp.size()) {
        if (comp[c].size() == 1) {
            ans.push_back(comp[c][0]);
            continue;
        }
        vector<int> es;
        {
            vector<bool> seen(n, false);
            rep (i, n) {
                auto dfs = [&](auto&& self, int v) -> void {
                    seen[v] = true;
                    for (auto e : g1[v]) if (!seen[e.to] && cmpid[e.idx] != c) {
                        es.push_back(e.idx);
                        self(self, e.to);
                    }
                };
                if (!seen[i]) {
                    dfs(dfs, i);
                }
            }
        }
        vector<int> U, V;
        int n2 = 0;
        {
            vector<int> a;
            for (auto i : comp[c]) {
                a.push_back(u[i]);
                a.push_back(v[i]);
            }
            sort(all(a));
            a.erase(unique(all(a)), end(a));
            for (auto i : comp[c]) {
                U.push_back(lower_bound(all(a), u[i]) - begin(a));
                V.push_back(lower_bound(all(a), v[i]) - begin(a));
            }
            n2 = a.size();
        }
        vector<vector<edge>> g(n2);
        rep (i, U.size()) {
            g[U[i]].push_back({V[i], i});
            g[V[i]].push_back({U[i], i});
        }
        vector<int> es2;
        vector<int> dep(n2, -1), par(n2, -1), pe(n2, -1);
        {
            auto dfs = [&](auto&& self, int v, int p) -> void {
                for (auto e : g[v]) if (dep[e.to] == -1) {
                    es2.push_back(e.idx);
                    dep[e.to] = dep[v] + 1;
                    par[e.to] = v;
                    pe[e.to] = e.idx;
                    self(self, e.to, v);
                }
            };
            dep[0] = 0;
            dfs(dfs, 0, -1);
        }
        auto lca = [&](int u, int v) -> int {
            while (u != v) {
                if (dep[u] < dep[v]) swap(u, v);
                u = par[u];
            }
            return u;
        };
        UnionFind uf(comp[c].size());
        auto es_ = es;
        for (auto i : es2) es.push_back(comp[c][i]);
        int c1 = Query(es);
        rep (i, comp[c].size()) {
            if (count(all(es2), i)) continue;
            int l = lca(U[i], V[i]);
            if (dep[U[i]] < dep[V[i]]) swap(U[i], V[i]);
            int v = U[i];
            vector<int> pes;
            while (v != l) {
                pes.push_back(pe[v]);
                v = par[v];
            }
            if (pes.empty() || uf.same(pes.front(), pes.back())) continue;
            v = U[i];
            while (v != l) {
                int j = i, k = pe[v];
                if (!uf.same(j, k)) {
                    auto es2 = es;
                    es2.erase(find(all(es2), comp[c][k]));
                    es2.push_back(comp[c][j]);
                    int c2 = Query(es2);
                    uf.merge(j, k, c2 - c1);
                }
                v = par[v];
            }
        }
        int mn = 0;
        int r = pe[1];
        rep (i, comp[c].size()) if (uf.same(r, i)) chmin(mn, uf.weight(i));
        vector<bool> is_common(comp[c].size(), false);
        vector<int> used_es;
        rep (i, comp[c].size()) if (uf.same(r, i)) {
            if (uf.weight(i) != mn) {
                ans.push_back(comp[c][i]);
                is_common[i] = true;
            }
            g[U[i]].erase(find(all(g[U[i]]), edge{V[i], i}));
            g[V[i]].erase(find(all(g[V[i]]), edge{U[i], i}));
            used_es.push_back(i);
        }

        // for (auto i : comp[c]) cout << i << " "; cout << endl;
        // for (auto i : used_es) cout << i << " "; cout << endl;
        // for (auto i : is_common) cout << i << " "; cout << endl;
        // for (auto i : es_) cout << i << " "; cout << endl;

        auto ask = [&](vector<int> es) {
            UnionFind uf(n2);
            for (auto i : es) uf.merge(U[i], V[i], 0);
            int cnt = 0;
            for (int i : used_es) {
                if (uf.merge(U[i], V[i], 0)) {
                    es.push_back(i);
                    if (is_common[i]) ++cnt;
                }
            }
            rep (i, n2) assert(uf.same(0, i));
            for (auto& i : es) i = comp[c][i];
            for (auto i : es_) es.push_back(i);
            return Query(es) - cnt;
        };
        int c2 = ask({});
        rep (i, n2) {
            vector<int> es;
            for (auto e : g[i]) es.push_back(e.idx);
            int t = ask(es) - c2;
            rep (i, t) {
                int ok = 0, ng = es.size();
                while (ng - ok > 1) {
                    int mid = (ok + ng) / 2;
                    vector<int> es2;
                    rep (j, mid) es2.push_back(es[j]);
                    if (ask(es2) - c2 > i) ng = mid;
                    else ok = mid;
                }
                ans.push_back(comp[c][es[ok]]);
            }
            for (auto e : g[i]) {
                g[e.to].erase(find(all(g[e.to]), edge{i, e.idx}));
            }
        }
    }
    sort(all(ans));
    ans.erase(unique(all(ans)), ans.end());
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 1 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 440 KB correct
12 Correct 1 ms 344 KB correct
13 Correct 1 ms 348 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 1 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 440 KB correct
12 Correct 1 ms 344 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 2 ms 696 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 3 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 2 ms 600 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 2 ms 344 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 344 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 604 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 608 KB correct
31 Correct 2 ms 604 KB correct
32 Correct 2 ms 604 KB correct
33 Correct 2 ms 552 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 1 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 440 KB correct
12 Correct 1 ms 344 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 2 ms 696 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 3 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 2 ms 600 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 2 ms 344 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 344 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 604 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 608 KB correct
31 Correct 2 ms 604 KB correct
32 Correct 2 ms 604 KB correct
33 Correct 2 ms 552 KB correct
34 Correct 73 ms 5732 KB correct
35 Correct 68 ms 5484 KB correct
36 Correct 57 ms 4972 KB correct
37 Correct 13 ms 1372 KB correct
38 Correct 69 ms 5740 KB correct
39 Correct 63 ms 5144 KB correct
40 Correct 53 ms 4536 KB correct
41 Correct 71 ms 5732 KB correct
42 Correct 70 ms 5548 KB correct
43 Correct 44 ms 4176 KB correct
44 Correct 36 ms 3120 KB correct
45 Correct 37 ms 3408 KB correct
46 Correct 32 ms 2944 KB correct
47 Correct 20 ms 2212 KB correct
48 Correct 6 ms 812 KB correct
49 Correct 15 ms 1628 KB correct
50 Correct 27 ms 2388 KB correct
51 Correct 43 ms 3780 KB correct
52 Correct 40 ms 3380 KB correct
53 Correct 38 ms 3480 KB correct
54 Correct 45 ms 4168 KB correct
55 Correct 50 ms 3676 KB correct
56 Correct 46 ms 3608 KB correct
57 Correct 53 ms 3796 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 252 ms 16160 KB correct
4 Correct 437 ms 23324 KB correct
5 Correct 455 ms 23580 KB correct
6 Correct 453 ms 22872 KB correct
7 Correct 402 ms 19760 KB correct
8 Correct 404 ms 20612 KB correct
9 Correct 430 ms 23416 KB correct
10 Correct 434 ms 23728 KB correct
11 Correct 477 ms 23576 KB correct
12 Correct 405 ms 23572 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 446 ms 22220 KB correct
15 Correct 442 ms 23580 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 1 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 1 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 440 KB correct
12 Correct 1 ms 344 KB correct
13 Correct 1 ms 348 KB correct
14 Correct 3 ms 604 KB correct
15 Correct 2 ms 604 KB correct
16 Correct 2 ms 696 KB correct
17 Correct 3 ms 604 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 3 ms 604 KB correct
20 Correct 2 ms 604 KB correct
21 Correct 2 ms 600 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 2 ms 344 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 1 ms 344 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 2 ms 604 KB correct
28 Correct 1 ms 348 KB correct
29 Correct 1 ms 344 KB correct
30 Correct 2 ms 608 KB correct
31 Correct 2 ms 604 KB correct
32 Correct 2 ms 604 KB correct
33 Correct 2 ms 552 KB correct
34 Correct 73 ms 5732 KB correct
35 Correct 68 ms 5484 KB correct
36 Correct 57 ms 4972 KB correct
37 Correct 13 ms 1372 KB correct
38 Correct 69 ms 5740 KB correct
39 Correct 63 ms 5144 KB correct
40 Correct 53 ms 4536 KB correct
41 Correct 71 ms 5732 KB correct
42 Correct 70 ms 5548 KB correct
43 Correct 44 ms 4176 KB correct
44 Correct 36 ms 3120 KB correct
45 Correct 37 ms 3408 KB correct
46 Correct 32 ms 2944 KB correct
47 Correct 20 ms 2212 KB correct
48 Correct 6 ms 812 KB correct
49 Correct 15 ms 1628 KB correct
50 Correct 27 ms 2388 KB correct
51 Correct 43 ms 3780 KB correct
52 Correct 40 ms 3380 KB correct
53 Correct 38 ms 3480 KB correct
54 Correct 45 ms 4168 KB correct
55 Correct 50 ms 3676 KB correct
56 Correct 46 ms 3608 KB correct
57 Correct 53 ms 3796 KB correct
58 Correct 0 ms 344 KB correct
59 Correct 1 ms 348 KB correct
60 Correct 252 ms 16160 KB correct
61 Correct 437 ms 23324 KB correct
62 Correct 455 ms 23580 KB correct
63 Correct 453 ms 22872 KB correct
64 Correct 402 ms 19760 KB correct
65 Correct 404 ms 20612 KB correct
66 Correct 430 ms 23416 KB correct
67 Correct 434 ms 23728 KB correct
68 Correct 477 ms 23576 KB correct
69 Correct 405 ms 23572 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 446 ms 22220 KB correct
72 Correct 442 ms 23580 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 443 ms 23748 KB correct
75 Correct 429 ms 23328 KB correct
76 Correct 181 ms 11216 KB correct
77 Correct 435 ms 23744 KB correct
78 Correct 438 ms 23840 KB correct
79 Correct 434 ms 23996 KB correct
80 Correct 392 ms 23600 KB correct
81 Correct 331 ms 19128 KB correct
82 Correct 395 ms 23260 KB correct
83 Correct 307 ms 16400 KB correct
84 Correct 226 ms 17012 KB correct
85 Correct 211 ms 14952 KB correct
86 Correct 152 ms 12256 KB correct
87 Correct 119 ms 11132 KB correct
88 Correct 116 ms 9812 KB correct
89 Correct 120 ms 9220 KB correct
90 Correct 101 ms 9044 KB correct
91 Correct 40 ms 4360 KB correct
92 Correct 22 ms 1872 KB correct
93 Correct 223 ms 16864 KB correct
94 Correct 210 ms 13220 KB correct
95 Correct 176 ms 13036 KB correct
96 Correct 129 ms 10048 KB correct
97 Correct 144 ms 10388 KB correct
98 Correct 155 ms 12004 KB correct
99 Correct 164 ms 10468 KB correct
100 Correct 83 ms 6328 KB correct
101 Correct 23 ms 2392 KB correct
102 Correct 237 ms 15512 KB correct
103 Correct 270 ms 15132 KB correct
104 Correct 295 ms 15340 KB correct
105 Correct 257 ms 14840 KB correct