Submission #1052824

# Submission time Handle Problem Language Result Execution time Memory
1052824 2024-08-11T02:24:55 Z becaido Simurgh (IOI17_simurgh) C++17
100 / 100
141 ms 7320 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#include "grader.cpp"
#else
#include "simurgh.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 505;
const int MSIZ = SIZE * SIZE / 2;

int n, m;
pair<int, int> ed[MSIZ];
vector<pair<int, int>> adj[SIZE];
int h[SIZE], pa[SIZE], pid[SIZE];

vector<int> tree;
bool is_tree[MSIZ];
int tree_cnt, ty[MSIZ];

struct DSU {
    int n;
    vector<int> to, h;
    void init(int n_) {
        n = n_;
        to.resize(n + 1);
        h.resize(n + 1);
        iota(to.begin() + 1, to.end(), 1);
        fill(h.begin() + 1, h.end(), 0);
    }
    int dsu(int x) {
        return x == to[x] ? x : (to[x] = dsu(to[x]));
    }
    bool merge(int a, int b, bool f = 0) {
        a = dsu(a), b = dsu(b);
        if (a == b) return 0;
        if (h[a] < h[b]) swap(a, b);
        to[b] = a;
        h[a] += (h[a] == h[b]);
        if (f) ty[a] = ty[b] = (ty[a] < 2 ? ty[a] : ty[b]);
        return 1;
    }
} mdsu;

void dfs(int pos) {
    for (auto [np, id] : adj[pos]) if (h[np] == 0) {
        h[np] = h[pos] + 1;
        pa[np] = pos;
        pid[np] = id;
        tree.pb(id);
        is_tree[id] = 1;
        dfs(np);
    }
}
int que(vector<int> e) {
    for (int &id : e) id--;
    return count_common_roads(e);
}
int ask(int x, int y) {
    vector<int> e;
    e.pb(y);
    for (int id : tree) if (x != id) e.pb(id);
    return que(e);
}
int ask_forest(vector<int> e) {
    DSU g;
    g.init(n);
    for (int id : e) {
        auto [x, y] = ed[id];
        g.merge(x, y);
    }
    int cnt = 0;
    for (int id : tree) {
        auto [x, y] = ed[id];
        if (g.merge(x, y)) {
            e.pb(id);
            cnt -= ty[id];
        }
    }
    cnt += que(e);
    return cnt;
}

vector<int> find_roads(int n_, vector<int> u, vector<int> v) {
    n = n_, m = u.size();
    FOR (i, 1, n) {
        adj[i].clear();
        h[i] = pa[i] = pid[i] = 0;
    }
    u.insert(u.begin(), 0);
    v.insert(v.begin(), 0);
    FOR (i, 1, m) {
        is_tree[i] = 0;
        u[i]++, v[i]++;
        ed[i] = {u[i], v[i]};
        adj[u[i]].pb(v[i], i);
        adj[v[i]].pb(u[i], i);
    }
    tree.clear();
    h[1] = 1, dfs(1);
    tree_cnt = que(tree);
    fill(ty + 1, ty + m + 1, 2);
    mdsu.init(m);
    FOR (i, 1, m) if (is_tree[i] == 0) {
        int a = u[i], b = v[i];
        if (h[a] > h[b]) swap(a, b);
        vector<int> e;
        int o = 0;
        while (h[a] < h[b]) {
            if (ty[mdsu.dsu(pid[b])] == 2) e.pb(pid[b]);
            else o = pid[b];
            b = pa[b];
        }
        if (e.size() == 0) continue;
        for (int j : e) {
            int cnt = ask(j, i);
            if (tree_cnt == cnt) mdsu.merge(i, j, 1);
            else if (tree_cnt < cnt) ty[mdsu.dsu(i)] = 1, ty[mdsu.dsu(j)] = 0;
            else ty[mdsu.dsu(i)] = 0, ty[mdsu.dsu(j)] = 1;
        }
        if (ty[mdsu.dsu(i)] == 2) {
            if (o == 0) ty[mdsu.dsu(i)] = 0;
            else {
                int cnt = ask(o, i);
                if (tree_cnt == cnt) mdsu.merge(o, i, 1);
                else ty[mdsu.dsu(i)] = (tree_cnt < cnt);
            }
        }
    }
    for (int id : tree) if (ty[mdsu.dsu(id)] == 2) ty[mdsu.dsu(id)] = 1;
    FOR (i, 1, m) ty[i] = ty[mdsu.dsu(i)];

    DSU g;
    g.init(n);
    for (int id : tree) {
        auto [x, y] = ed[id];
        if (ty[id] == 1) g.merge(x, y);
    }
    FOR (i, 1, n) {
        vector<int> e;
        for (auto [j, id] : adj[i]) {
            auto [x, y] = ed[id];
            if (ty[id] == 2 && g.dsu(x) != g.dsu(y)) e.pb(id);
        }
        if (e.size() == 0) continue;
        auto rec = [&](auto rec, int l, int r, int tot)->void {
            if (tot == 0) {
                FOR (j, l, r) ty[e[j]] = 0;
                return;
            }
            if (tot == r - l + 1) {
                FOR (j, l, r) {
                    auto [x, y] = ed[e[j]];
                    ty[e[j]] = 1;
                    g.merge(x, y);
                }
                return;
            }
            int mid = (l + r) / 2, lc;
            vector<int> le;
            FOR (j, l, mid) le.pb(e[j]);
            lc = ask_forest(le);
            rec(rec, l, mid, lc);
            rec(rec, mid + 1, r, tot - lc);
        };
        rec(rec, 0, e.size() - 1, ask_forest(e));
    }
    vector<int> ans;
    FOR (i, 1, m) if (ty[i] == 1) ans.pb(i - 1);
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 344 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 420 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 344 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 344 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 344 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 420 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 344 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 0 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 556 KB correct
18 Correct 0 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 0 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 0 ms 348 KB correct
27 Correct 0 ms 344 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 0 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 344 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 344 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 420 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 344 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 0 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 556 KB correct
18 Correct 0 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 0 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 0 ms 348 KB correct
27 Correct 0 ms 344 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 0 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 344 KB correct
34 Correct 27 ms 2044 KB correct
35 Correct 21 ms 2052 KB correct
36 Correct 19 ms 1764 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 24 ms 2108 KB correct
39 Correct 17 ms 1820 KB correct
40 Correct 14 ms 1604 KB correct
41 Correct 19 ms 2040 KB correct
42 Correct 21 ms 2116 KB correct
43 Correct 9 ms 1372 KB correct
44 Correct 7 ms 1116 KB correct
45 Correct 8 ms 1116 KB correct
46 Correct 6 ms 1116 KB correct
47 Correct 5 ms 600 KB correct
48 Correct 1 ms 348 KB correct
49 Correct 2 ms 348 KB correct
50 Correct 4 ms 604 KB correct
51 Correct 9 ms 1300 KB correct
52 Correct 8 ms 1228 KB correct
53 Correct 7 ms 1112 KB correct
54 Correct 9 ms 1372 KB correct
55 Correct 9 ms 1340 KB correct
56 Correct 9 ms 1116 KB correct
57 Correct 9 ms 1116 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 67 ms 5100 KB correct
4 Correct 118 ms 7128 KB correct
5 Correct 120 ms 7312 KB correct
6 Correct 112 ms 7304 KB correct
7 Correct 91 ms 7308 KB correct
8 Correct 98 ms 7304 KB correct
9 Correct 121 ms 7320 KB correct
10 Correct 123 ms 7308 KB correct
11 Correct 126 ms 7308 KB correct
12 Correct 120 ms 7316 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 115 ms 7132 KB correct
15 Correct 141 ms 7304 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 344 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 420 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 344 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 1 ms 344 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 0 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 556 KB correct
18 Correct 0 ms 344 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 0 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 0 ms 348 KB correct
27 Correct 0 ms 344 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 0 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 344 KB correct
34 Correct 27 ms 2044 KB correct
35 Correct 21 ms 2052 KB correct
36 Correct 19 ms 1764 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 24 ms 2108 KB correct
39 Correct 17 ms 1820 KB correct
40 Correct 14 ms 1604 KB correct
41 Correct 19 ms 2040 KB correct
42 Correct 21 ms 2116 KB correct
43 Correct 9 ms 1372 KB correct
44 Correct 7 ms 1116 KB correct
45 Correct 8 ms 1116 KB correct
46 Correct 6 ms 1116 KB correct
47 Correct 5 ms 600 KB correct
48 Correct 1 ms 348 KB correct
49 Correct 2 ms 348 KB correct
50 Correct 4 ms 604 KB correct
51 Correct 9 ms 1300 KB correct
52 Correct 8 ms 1228 KB correct
53 Correct 7 ms 1112 KB correct
54 Correct 9 ms 1372 KB correct
55 Correct 9 ms 1340 KB correct
56 Correct 9 ms 1116 KB correct
57 Correct 9 ms 1116 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 67 ms 5100 KB correct
61 Correct 118 ms 7128 KB correct
62 Correct 120 ms 7312 KB correct
63 Correct 112 ms 7304 KB correct
64 Correct 91 ms 7308 KB correct
65 Correct 98 ms 7304 KB correct
66 Correct 121 ms 7320 KB correct
67 Correct 123 ms 7308 KB correct
68 Correct 126 ms 7308 KB correct
69 Correct 120 ms 7316 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 115 ms 7132 KB correct
72 Correct 141 ms 7304 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 119 ms 7128 KB correct
75 Correct 118 ms 7156 KB correct
76 Correct 48 ms 2816 KB correct
77 Correct 126 ms 7304 KB correct
78 Correct 124 ms 7308 KB correct
79 Correct 139 ms 7308 KB correct
80 Correct 119 ms 7176 KB correct
81 Correct 84 ms 6460 KB correct
82 Correct 117 ms 7084 KB correct
83 Correct 76 ms 4208 KB correct
84 Correct 50 ms 4992 KB correct
85 Correct 45 ms 4700 KB correct
86 Correct 33 ms 3064 KB correct
87 Correct 26 ms 2536 KB correct
88 Correct 21 ms 1820 KB correct
89 Correct 21 ms 1868 KB correct
90 Correct 19 ms 1604 KB correct
91 Correct 7 ms 856 KB correct
92 Correct 3 ms 348 KB correct
93 Correct 53 ms 4700 KB correct
94 Correct 37 ms 3068 KB correct
95 Correct 31 ms 3088 KB correct
96 Correct 20 ms 1848 KB correct
97 Correct 22 ms 2020 KB correct
98 Correct 25 ms 2632 KB correct
99 Correct 21 ms 1804 KB correct
100 Correct 11 ms 860 KB correct
101 Correct 4 ms 600 KB correct
102 Correct 53 ms 3984 KB correct
103 Correct 55 ms 3972 KB correct
104 Correct 62 ms 3696 KB correct
105 Correct 53 ms 3700 KB correct