Submission #847485

# Submission time Handle Problem Language Result Execution time Memory
847485 2023-09-09T18:02:46 Z ArashJafariyan Stranded Far From Home (BOI22_island) C++17
100 / 100
164 ms 28228 KB
#include <bits/stdc++.h>
using ll = long long;
 
#define pb push_back
 
const int N = 2e5 + 4;
 
int n, m, st[N], a[N], b[N], idx[N], par[N];
ll sum[N];
char ans[N];
std::vector<int> vset[N];
 
bool cmp(int i, int j) {
    int x = std::max(st[a[i]], st[b[i]]);
    int y = std::max(st[a[j]], st[b[j]]);
    return x < y;
}
 
int gr(int v) {
    std::stack<int> s;
    s.push(v);
    while (par[s.top()] != s.top()) {
        s.push(par[s.top()]);
    }
 
    int ret = s.top();
    while (!s.empty()) {
        par[s.top()] = ret;
        s.pop();
    }
    return ret;
}
 
void mrg(int v, int u, int w) {
    v = gr(v);
    u = gr(u);
    if (v == u) {
        return;
    }
    if (sum[v] > sum[u]) {
        std::swap(v, u);
    }
 
    bool b = sum[v] < w;
    for (int x : vset[v]) {
        vset[u].pb(x);
        if (b) {
            ans[x] = '0';
        }
    }
    sum[u] += sum[v];
    par[v] = u;
    return;
}
 
int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
 
    std::cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        std::cin >> sum[i];
        st[i] = sum[i];
        vset[i].pb(i);
        ans[i] = '1';
        par[i] = i;
    }
    for (int i = 0; i < m; ++i) {
        std::cin >> a[i] >> b[i];
        --a[i], --b[i];
        idx[i] = i;
    }
    std::sort(idx, idx + m, cmp); 
    
    for (int i = 0; i < m; ++i) {
        int j = idx[i];
        int w = std::max(st[a[j]], st[b[j]]);
        mrg(a[j], b[j], w);
    }
    for (int i = 0; i < n; ++i) {
        std::cout << ans[i];
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 3 ms 8792 KB Output is correct
13 Correct 3 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 1 ms 8792 KB Output is correct
3 Correct 106 ms 18384 KB Output is correct
4 Correct 105 ms 28092 KB Output is correct
5 Correct 129 ms 22324 KB Output is correct
6 Correct 130 ms 22336 KB Output is correct
7 Correct 131 ms 22752 KB Output is correct
8 Correct 127 ms 21844 KB Output is correct
9 Correct 117 ms 26944 KB Output is correct
10 Correct 105 ms 18124 KB Output is correct
11 Correct 98 ms 18120 KB Output is correct
12 Correct 125 ms 23560 KB Output is correct
13 Correct 112 ms 26176 KB Output is correct
14 Correct 109 ms 27520 KB Output is correct
15 Correct 100 ms 18208 KB Output is correct
16 Correct 69 ms 18132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 143 ms 24812 KB Output is correct
3 Correct 139 ms 24272 KB Output is correct
4 Correct 111 ms 24644 KB Output is correct
5 Correct 106 ms 25920 KB Output is correct
6 Correct 148 ms 24512 KB Output is correct
7 Correct 101 ms 18120 KB Output is correct
8 Correct 100 ms 18120 KB Output is correct
9 Correct 68 ms 18140 KB Output is correct
10 Correct 131 ms 24680 KB Output is correct
11 Correct 115 ms 27596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 123 ms 20244 KB Output is correct
3 Correct 137 ms 21196 KB Output is correct
4 Correct 132 ms 21960 KB Output is correct
5 Correct 136 ms 21220 KB Output is correct
6 Correct 120 ms 19400 KB Output is correct
7 Correct 102 ms 18172 KB Output is correct
8 Correct 87 ms 20296 KB Output is correct
9 Correct 79 ms 15072 KB Output is correct
10 Correct 122 ms 23848 KB Output is correct
11 Correct 140 ms 28228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8792 KB Output is correct
2 Correct 2 ms 8792 KB Output is correct
3 Correct 2 ms 8792 KB Output is correct
4 Correct 2 ms 8792 KB Output is correct
5 Correct 2 ms 8792 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 2 ms 8792 KB Output is correct
10 Correct 2 ms 8792 KB Output is correct
11 Correct 2 ms 8792 KB Output is correct
12 Correct 3 ms 8792 KB Output is correct
13 Correct 3 ms 8792 KB Output is correct
14 Correct 2 ms 8792 KB Output is correct
15 Correct 2 ms 8792 KB Output is correct
16 Correct 1 ms 8792 KB Output is correct
17 Correct 106 ms 18384 KB Output is correct
18 Correct 105 ms 28092 KB Output is correct
19 Correct 129 ms 22324 KB Output is correct
20 Correct 130 ms 22336 KB Output is correct
21 Correct 131 ms 22752 KB Output is correct
22 Correct 127 ms 21844 KB Output is correct
23 Correct 117 ms 26944 KB Output is correct
24 Correct 105 ms 18124 KB Output is correct
25 Correct 98 ms 18120 KB Output is correct
26 Correct 125 ms 23560 KB Output is correct
27 Correct 112 ms 26176 KB Output is correct
28 Correct 109 ms 27520 KB Output is correct
29 Correct 100 ms 18208 KB Output is correct
30 Correct 69 ms 18132 KB Output is correct
31 Correct 2 ms 8792 KB Output is correct
32 Correct 143 ms 24812 KB Output is correct
33 Correct 139 ms 24272 KB Output is correct
34 Correct 111 ms 24644 KB Output is correct
35 Correct 106 ms 25920 KB Output is correct
36 Correct 148 ms 24512 KB Output is correct
37 Correct 101 ms 18120 KB Output is correct
38 Correct 100 ms 18120 KB Output is correct
39 Correct 68 ms 18140 KB Output is correct
40 Correct 131 ms 24680 KB Output is correct
41 Correct 115 ms 27596 KB Output is correct
42 Correct 2 ms 8792 KB Output is correct
43 Correct 123 ms 20244 KB Output is correct
44 Correct 137 ms 21196 KB Output is correct
45 Correct 132 ms 21960 KB Output is correct
46 Correct 136 ms 21220 KB Output is correct
47 Correct 120 ms 19400 KB Output is correct
48 Correct 102 ms 18172 KB Output is correct
49 Correct 87 ms 20296 KB Output is correct
50 Correct 79 ms 15072 KB Output is correct
51 Correct 122 ms 23848 KB Output is correct
52 Correct 140 ms 28228 KB Output is correct
53 Correct 2 ms 8792 KB Output is correct
54 Correct 1 ms 8792 KB Output is correct
55 Correct 2 ms 8796 KB Output is correct
56 Correct 3 ms 8792 KB Output is correct
57 Correct 2 ms 8792 KB Output is correct
58 Correct 2 ms 9048 KB Output is correct
59 Correct 2 ms 8792 KB Output is correct
60 Correct 2 ms 8792 KB Output is correct
61 Correct 2 ms 8792 KB Output is correct
62 Correct 2 ms 8792 KB Output is correct
63 Correct 3 ms 8796 KB Output is correct
64 Correct 2 ms 8792 KB Output is correct
65 Correct 2 ms 8792 KB Output is correct
66 Correct 2 ms 8792 KB Output is correct
67 Correct 104 ms 18460 KB Output is correct
68 Correct 101 ms 28032 KB Output is correct
69 Correct 132 ms 21964 KB Output is correct
70 Correct 133 ms 22336 KB Output is correct
71 Correct 132 ms 22468 KB Output is correct
72 Correct 110 ms 22084 KB Output is correct
73 Correct 112 ms 26944 KB Output is correct
74 Correct 109 ms 18120 KB Output is correct
75 Correct 87 ms 18124 KB Output is correct
76 Correct 114 ms 23364 KB Output is correct
77 Correct 105 ms 26012 KB Output is correct
78 Correct 107 ms 27592 KB Output is correct
79 Correct 105 ms 18032 KB Output is correct
80 Correct 70 ms 18116 KB Output is correct
81 Correct 145 ms 24644 KB Output is correct
82 Correct 153 ms 24384 KB Output is correct
83 Correct 109 ms 24596 KB Output is correct
84 Correct 126 ms 25932 KB Output is correct
85 Correct 164 ms 24548 KB Output is correct
86 Correct 101 ms 18108 KB Output is correct
87 Correct 103 ms 18124 KB Output is correct
88 Correct 107 ms 24760 KB Output is correct
89 Correct 118 ms 27592 KB Output is correct
90 Correct 130 ms 20040 KB Output is correct
91 Correct 123 ms 20896 KB Output is correct
92 Correct 126 ms 21996 KB Output is correct
93 Correct 120 ms 21476 KB Output is correct
94 Correct 102 ms 19376 KB Output is correct
95 Correct 108 ms 18176 KB Output is correct
96 Correct 79 ms 20292 KB Output is correct
97 Correct 76 ms 15052 KB Output is correct
98 Correct 117 ms 23492 KB Output is correct
99 Correct 117 ms 28056 KB Output is correct
100 Correct 63 ms 10072 KB Output is correct
101 Correct 158 ms 21496 KB Output is correct
102 Correct 97 ms 17352 KB Output is correct
103 Correct 128 ms 17608 KB Output is correct
104 Correct 158 ms 19476 KB Output is correct