Submission #695208

# Submission time Handle Problem Language Result Execution time Memory
695208 2023-02-04T19:25:39 Z finn__ Stranded Far From Home (BOI22_island) C++17
100 / 100
219 ms 43428 KB
#include <bits/stdc++.h>
using namespace std;

struct DSU
{
    vector<int64_t> parent, weight;
    vector<vector<int64_t>> components;

    DSU(size_t n, vector<int64_t> const &s)
    {
        parent = vector<int64_t>(n, -1);
        weight = vector<int64_t>(s.begin(), s.end());
        components = vector<vector<int64_t>>(n);
        for (size_t i = 0; i < n; i++)
            components[i].push_back(i);
    }

    int root(int u)
    {
        return parent[u] < 0 ? u : parent[u] = root(parent[u]);
    }

    void merge(int u, int v)
    {
        u = root(u), v = root(v);
        if (u == v)
            return;

        if (parent[u] > parent[v])
            swap(u, v);

        weight[u] += weight[v];
        parent[u] += parent[v];
        parent[v] = u;
        components[u].insert(components[u].end(), components[v].begin(), components[v].end());
    }

    int64_t component_weight(int u) { return weight[root(u)]; }
};

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    size_t n, m;
    cin >> n >> m;

    vector<int64_t> s(n);
    for (int64_t &x : s)
        cin >> x;

    vector<pair<int64_t, unsigned>> nodes(n);
    for (size_t i = 0; i < n; i++)
        nodes[i] = {s[i], i};
    sort(nodes.begin(), nodes.end());

    vector<vector<unsigned>> g(n);
    for (size_t i = 0; i < m; i++)
    {
        unsigned u, v;
        cin >> u >> v;
        g[u - 1].push_back(v - 1);
        g[v - 1].push_back(u - 1);
    }

    DSU dsu(n, s);

    for (auto const &[x, u] : nodes)
    {
        for (unsigned const &v : g[u])
        {
            if (s[v] <= x)
            {
                if (dsu.component_weight(v) < x)
                    dsu.components[dsu.root(v)].clear();
                dsu.merge(u, v);
            }
        }
    }

    string ans(n, '0');
    for (unsigned i : dsu.components[dsu.root(nodes.back().second)])
        ans[i] = '1';
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 1 ms 552 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 129 ms 32608 KB Output is correct
4 Correct 134 ms 34496 KB Output is correct
5 Correct 210 ms 39676 KB Output is correct
6 Correct 163 ms 40712 KB Output is correct
7 Correct 158 ms 40780 KB Output is correct
8 Correct 176 ms 36528 KB Output is correct
9 Correct 138 ms 43044 KB Output is correct
10 Correct 101 ms 34792 KB Output is correct
11 Correct 129 ms 36352 KB Output is correct
12 Correct 151 ms 35736 KB Output is correct
13 Correct 135 ms 35096 KB Output is correct
14 Correct 148 ms 35252 KB Output is correct
15 Correct 130 ms 36424 KB Output is correct
16 Correct 84 ms 35648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 183 ms 37764 KB Output is correct
3 Correct 186 ms 42000 KB Output is correct
4 Correct 142 ms 36724 KB Output is correct
5 Correct 127 ms 35268 KB Output is correct
6 Correct 182 ms 42076 KB Output is correct
7 Correct 144 ms 36724 KB Output is correct
8 Correct 146 ms 36636 KB Output is correct
9 Correct 82 ms 35616 KB Output is correct
10 Correct 123 ms 35016 KB Output is correct
11 Correct 138 ms 35980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 204 ms 33420 KB Output is correct
3 Correct 190 ms 38664 KB Output is correct
4 Correct 198 ms 40472 KB Output is correct
5 Correct 218 ms 42244 KB Output is correct
6 Correct 177 ms 38688 KB Output is correct
7 Correct 119 ms 37312 KB Output is correct
8 Correct 86 ms 34500 KB Output is correct
9 Correct 102 ms 20000 KB Output is correct
10 Correct 175 ms 43388 KB Output is correct
11 Correct 141 ms 36076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 1 ms 552 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 632 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 129 ms 32608 KB Output is correct
18 Correct 134 ms 34496 KB Output is correct
19 Correct 210 ms 39676 KB Output is correct
20 Correct 163 ms 40712 KB Output is correct
21 Correct 158 ms 40780 KB Output is correct
22 Correct 176 ms 36528 KB Output is correct
23 Correct 138 ms 43044 KB Output is correct
24 Correct 101 ms 34792 KB Output is correct
25 Correct 129 ms 36352 KB Output is correct
26 Correct 151 ms 35736 KB Output is correct
27 Correct 135 ms 35096 KB Output is correct
28 Correct 148 ms 35252 KB Output is correct
29 Correct 130 ms 36424 KB Output is correct
30 Correct 84 ms 35648 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 183 ms 37764 KB Output is correct
33 Correct 186 ms 42000 KB Output is correct
34 Correct 142 ms 36724 KB Output is correct
35 Correct 127 ms 35268 KB Output is correct
36 Correct 182 ms 42076 KB Output is correct
37 Correct 144 ms 36724 KB Output is correct
38 Correct 146 ms 36636 KB Output is correct
39 Correct 82 ms 35616 KB Output is correct
40 Correct 123 ms 35016 KB Output is correct
41 Correct 138 ms 35980 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 204 ms 33420 KB Output is correct
44 Correct 190 ms 38664 KB Output is correct
45 Correct 198 ms 40472 KB Output is correct
46 Correct 218 ms 42244 KB Output is correct
47 Correct 177 ms 38688 KB Output is correct
48 Correct 119 ms 37312 KB Output is correct
49 Correct 86 ms 34500 KB Output is correct
50 Correct 102 ms 20000 KB Output is correct
51 Correct 175 ms 43388 KB Output is correct
52 Correct 141 ms 36076 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 0 ms 320 KB Output is correct
56 Correct 2 ms 596 KB Output is correct
57 Correct 2 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 2 ms 596 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 2 ms 624 KB Output is correct
62 Correct 1 ms 724 KB Output is correct
63 Correct 2 ms 724 KB Output is correct
64 Correct 1 ms 596 KB Output is correct
65 Correct 2 ms 584 KB Output is correct
66 Correct 2 ms 596 KB Output is correct
67 Correct 132 ms 37076 KB Output is correct
68 Correct 126 ms 35296 KB Output is correct
69 Correct 148 ms 39492 KB Output is correct
70 Correct 153 ms 40932 KB Output is correct
71 Correct 167 ms 40820 KB Output is correct
72 Correct 151 ms 36704 KB Output is correct
73 Correct 133 ms 43196 KB Output is correct
74 Correct 96 ms 34756 KB Output is correct
75 Correct 142 ms 36344 KB Output is correct
76 Correct 161 ms 35676 KB Output is correct
77 Correct 118 ms 35052 KB Output is correct
78 Correct 138 ms 35336 KB Output is correct
79 Correct 124 ms 36680 KB Output is correct
80 Correct 82 ms 35644 KB Output is correct
81 Correct 201 ms 42200 KB Output is correct
82 Correct 182 ms 42004 KB Output is correct
83 Correct 149 ms 36616 KB Output is correct
84 Correct 123 ms 35172 KB Output is correct
85 Correct 186 ms 42144 KB Output is correct
86 Correct 141 ms 36600 KB Output is correct
87 Correct 151 ms 36712 KB Output is correct
88 Correct 127 ms 35016 KB Output is correct
89 Correct 137 ms 35944 KB Output is correct
90 Correct 191 ms 37892 KB Output is correct
91 Correct 182 ms 38612 KB Output is correct
92 Correct 198 ms 40576 KB Output is correct
93 Correct 219 ms 42280 KB Output is correct
94 Correct 174 ms 38632 KB Output is correct
95 Correct 118 ms 37408 KB Output is correct
96 Correct 91 ms 34508 KB Output is correct
97 Correct 105 ms 20000 KB Output is correct
98 Correct 190 ms 43428 KB Output is correct
99 Correct 142 ms 36064 KB Output is correct
100 Correct 36 ms 4804 KB Output is correct
101 Correct 193 ms 39300 KB Output is correct
102 Correct 155 ms 31016 KB Output is correct
103 Correct 161 ms 31064 KB Output is correct
104 Correct 188 ms 37180 KB Output is correct