답안 #425183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
425183 2021-06-12T14:45:32 Z timmyfeng Simurgh (IOI17_simurgh) C++17
100 / 100
340 ms 6596 KB
#include <bits/stdc++.h>
using namespace std;

#include "simurgh.h"

const int N = 500;

int par[N], edge[N], depth[N], deg[N], matches[N], dsu[N], n;
bool royal[N], visited[N], observed[N];
vector<int> span, ans, not_ans, u, v;
vector<array<int, 2>> adj[N];

void dfs(int u) {
    visited[u] = true;
    for (auto [c, e] : adj[u]) {
        if (!visited[c]) {
            span.push_back(e);
            depth[c] = depth[u] + 1;
            edge[c] = e;
            par[c] = u;
            dfs(c);
        }
    }
}

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

bool unite(int u, int v) {
    u = find(u), v = find(v);
    if (u != v) {
        if (-dsu[u] > -dsu[v]) {
            swap(u, v);
        }
        dsu[v] += dsu[u];
        dsu[u] = v;
        return true;
    }
    return false;
}

int get(vector<int> &tree) {
    fill(dsu, dsu + n, -1);

    for (auto i : tree) {
        unite(u[i], v[i]);
    }

    for (auto i : not_ans) {
        if (unite(u[i], v[i])) {
            tree.push_back(i);
        }
    }

    int offset = 0;
    for (auto i : ans) {
        if (unite(u[i], v[i])) {
            tree.push_back(i);
            ++offset;
        }
    }

    return offset;
}

vector<int> find_roads(int n, vector<int> u, vector<int> v) {
    int m = u.size(); ::n = n, ::u = u, ::v = v;

    for (int i = 0; i < m; ++i) {
        adj[u[i]].push_back({v[i], i});
        adj[v[i]].push_back({u[i], i});
    }

    dfs(0);

    int init = count_common_roads(span);
    for (int i = 0; i < m; ++i) {
        if (depth[u[i]] > depth[v[i]]) {
            swap(u[i], v[i]);
        }

        if (depth[u[i]] + 1 == depth[v[i]]) {
            continue;
        }

        bool learn = false;
        for (int j = v[i]; j != u[i]; j = par[j]) {
            learn |= !observed[j];
        }

        if (!learn) {
            continue;
        }

        int reference = -1, mini = n - 1, maxi = 0;
        for (int j = v[i]; j != u[i]; j = par[j]) {
            vector<int> cycle = span;
            cycle.erase(find(cycle.begin(), cycle.end(), edge[j]));
            cycle.push_back(i);

            int value = -1;
            if (!observed[j]) {
                value = matches[j] = count_common_roads(cycle);
            } else if (reference == -1) {
                value = count_common_roads(cycle);
                reference = j;
            }

            if (value != -1) {
                mini = min(mini, value);
                maxi = max(maxi, value);
            }
        }

        for (int j = v[i]; j != u[i]; j = par[j]) {
            if (!observed[j]) {
                observed[j] = true;
                if (mini == maxi) {
                    if (reference == -1) {
                        royal[j] = mini < init;
                    } else {
                        royal[j] = royal[reference];
                    }
                } else {
                    royal[j] = matches[j] == mini;
                }
            }
        }
    }

    for (int i = 1; i < n; ++i) {
        (royal[i] || !observed[i] ? ans : not_ans).push_back(edge[i]);
    }

    vector<int> stk;
    for (int i = 0; i < n; ++i) {
        vector<int> star;
        for (auto [j, e] : adj[i]) {
            if (count(ans.begin(), ans.end(), e) == 0) {
                star.push_back(e);
            }
        }
        int offset = get(star);

        deg[i] = count_common_roads(star) - offset;
        if (deg[i] == 1) {
            stk.push_back(i);
        }
    }

    while (!stk.empty()) {
        int i = stk.back();
        stk.pop_back();
        if (deg[i] == 0) {
            continue;
        }

        vector<int> candidates;
        for (auto [j, e] : adj[i]) {
            if (count(ans.begin(), ans.end(), e) == 0) {
                candidates.push_back(e);
            }
        }

        int low = 0, high = candidates.size() - 1;
        while (low < high) {
            int mid = (low + high) / 2;

            vector<int> star = vector<int>(candidates.begin(), candidates.begin() + mid + 1);
            int offset = get(star);

            if (count_common_roads(star) - offset > 0) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }

        int e = candidates[low], j = i ^ u[e] ^ v[e];
        ans.push_back(e);
        if (--deg[j] == 1) {
            stk.push_back(j);
        }
    }

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 0 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 312 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 0 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 312 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 388 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 312 KB correct
23 Correct 1 ms 312 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 1 ms 312 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 0 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 312 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 388 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 312 KB correct
23 Correct 1 ms 312 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 1 ms 312 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 54 ms 1776 KB correct
35 Correct 53 ms 1736 KB correct
36 Correct 46 ms 1484 KB correct
37 Correct 13 ms 424 KB correct
38 Correct 65 ms 1764 KB correct
39 Correct 51 ms 1560 KB correct
40 Correct 46 ms 1484 KB correct
41 Correct 53 ms 1740 KB correct
42 Correct 54 ms 1780 KB correct
43 Correct 26 ms 1260 KB correct
44 Correct 22 ms 988 KB correct
45 Correct 24 ms 1060 KB correct
46 Correct 21 ms 844 KB correct
47 Correct 15 ms 628 KB correct
48 Correct 5 ms 332 KB correct
49 Correct 9 ms 428 KB correct
50 Correct 14 ms 588 KB correct
51 Correct 25 ms 1064 KB correct
52 Correct 22 ms 972 KB correct
53 Correct 22 ms 844 KB correct
54 Correct 25 ms 1276 KB correct
55 Correct 24 ms 972 KB correct
56 Correct 24 ms 1072 KB correct
57 Correct 24 ms 1080 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 187 ms 4052 KB correct
4 Correct 326 ms 5564 KB correct
5 Correct 320 ms 5560 KB correct
6 Correct 312 ms 5508 KB correct
7 Correct 321 ms 5632 KB correct
8 Correct 316 ms 5564 KB correct
9 Correct 324 ms 5572 KB correct
10 Correct 325 ms 5572 KB correct
11 Correct 321 ms 5608 KB correct
12 Correct 315 ms 5628 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 340 ms 5512 KB correct
15 Correct 313 ms 5592 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Correct 1 ms 204 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 1 ms 204 KB correct
7 Correct 0 ms 204 KB correct
8 Correct 0 ms 204 KB correct
9 Correct 0 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 204 KB correct
13 Correct 1 ms 312 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 2 ms 332 KB correct
17 Correct 2 ms 332 KB correct
18 Correct 2 ms 332 KB correct
19 Correct 2 ms 332 KB correct
20 Correct 2 ms 388 KB correct
21 Correct 2 ms 332 KB correct
22 Correct 2 ms 312 KB correct
23 Correct 1 ms 312 KB correct
24 Correct 1 ms 332 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 332 KB correct
29 Correct 1 ms 312 KB correct
30 Correct 1 ms 312 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 332 KB correct
34 Correct 54 ms 1776 KB correct
35 Correct 53 ms 1736 KB correct
36 Correct 46 ms 1484 KB correct
37 Correct 13 ms 424 KB correct
38 Correct 65 ms 1764 KB correct
39 Correct 51 ms 1560 KB correct
40 Correct 46 ms 1484 KB correct
41 Correct 53 ms 1740 KB correct
42 Correct 54 ms 1780 KB correct
43 Correct 26 ms 1260 KB correct
44 Correct 22 ms 988 KB correct
45 Correct 24 ms 1060 KB correct
46 Correct 21 ms 844 KB correct
47 Correct 15 ms 628 KB correct
48 Correct 5 ms 332 KB correct
49 Correct 9 ms 428 KB correct
50 Correct 14 ms 588 KB correct
51 Correct 25 ms 1064 KB correct
52 Correct 22 ms 972 KB correct
53 Correct 22 ms 844 KB correct
54 Correct 25 ms 1276 KB correct
55 Correct 24 ms 972 KB correct
56 Correct 24 ms 1072 KB correct
57 Correct 24 ms 1080 KB correct
58 Correct 0 ms 204 KB correct
59 Correct 1 ms 204 KB correct
60 Correct 187 ms 4052 KB correct
61 Correct 326 ms 5564 KB correct
62 Correct 320 ms 5560 KB correct
63 Correct 312 ms 5508 KB correct
64 Correct 321 ms 5632 KB correct
65 Correct 316 ms 5564 KB correct
66 Correct 324 ms 5572 KB correct
67 Correct 325 ms 5572 KB correct
68 Correct 321 ms 5608 KB correct
69 Correct 315 ms 5628 KB correct
70 Correct 0 ms 204 KB correct
71 Correct 340 ms 5512 KB correct
72 Correct 313 ms 5592 KB correct
73 Correct 1 ms 204 KB correct
74 Correct 316 ms 6488 KB correct
75 Correct 314 ms 6372 KB correct
76 Correct 151 ms 2652 KB correct
77 Correct 320 ms 6544 KB correct
78 Correct 320 ms 6596 KB correct
79 Correct 320 ms 6520 KB correct
80 Correct 329 ms 6384 KB correct
81 Correct 285 ms 5796 KB correct
82 Correct 306 ms 6372 KB correct
83 Correct 229 ms 3660 KB correct
84 Correct 166 ms 4440 KB correct
85 Correct 150 ms 4212 KB correct
86 Correct 119 ms 2804 KB correct
87 Correct 93 ms 2240 KB correct
88 Correct 80 ms 1672 KB correct
89 Correct 81 ms 1612 KB correct
90 Correct 73 ms 1512 KB correct
91 Correct 29 ms 520 KB correct
92 Correct 18 ms 332 KB correct
93 Correct 149 ms 4220 KB correct
94 Correct 115 ms 2764 KB correct
95 Correct 111 ms 2764 KB correct
96 Correct 78 ms 1584 KB correct
97 Correct 80 ms 1696 KB correct
98 Correct 93 ms 2348 KB correct
99 Correct 82 ms 1704 KB correct
100 Correct 43 ms 704 KB correct
101 Correct 21 ms 332 KB correct
102 Correct 146 ms 3384 KB correct
103 Correct 145 ms 3452 KB correct
104 Correct 153 ms 3436 KB correct
105 Correct 144 ms 3448 KB correct