Submission #578566

# Submission time Handle Problem Language Result Execution time Memory
578566 2022-06-17T09:51:31 Z Jarif_Rahman Stranded Far From Home (BOI22_island) C++17
100 / 100
164 ms 26796 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

void merge_vector(vector<int> &a, vector<int> &b){
    if(b.size() > a.size()) swap(a, b);
    while(!b.empty()) a.pb(b.back()), b.pop_back();
}

struct rechability_tree{
    int n;
    vector<int> p;
    vector<ll> s, sum;
    vector<vector<int>> sth;
    rechability_tree(int _n){
        n = _n;
        p.assign(n, -1);
        s.assign(n, 0);
        sum.assign(n, 0);
        sth.assign(n, {});
        for(int i = 0; i < n; i++) p[i] = i;
        for(int i = 0; i < n; i++) sth[i].pb(i);
    }
    int get(int nd){
        if(p[nd] != nd) p[nd] = get(p[nd]);
        return p[nd];
    }
    void unite(int a, int b){
        if(get(a) == get(b)) return;
        a = get(a), b = get(b);
        if(sum[b] >= s[a]) merge_vector(sth[a], sth[b]);
        sum[a]+=sum[b];
        p[b] = a;
    }
};

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

    int n, m; cin >> n >> m;

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

    rechability_tree rt(n);
    for(int i = 0; i < n; i++) rt.s[i] = s[i], rt.sum[i] = s[i];

    vector<pair<int, int>> edges;
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b; a--, b--;
        if(s[b] > s[a]) swap(a, b);
        edges.pb({a, b});
    }

    sort(edges.begin(), edges.end(), [&s](pair<int, int> A, pair<int, int> B){
        return make_pair(s[A.f], s[A.sc]) < make_pair(s[B.f], s[B.sc]);
    });

    for(auto [a, b]: edges) rt.unite(a, b);

    int p = rt.get(0);

    str ans(n, '0');
    for(int x: rt.sth[p]) ans[x] = '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 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 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 98 ms 20036 KB Output is correct
4 Correct 131 ms 25768 KB Output is correct
5 Correct 116 ms 21108 KB Output is correct
6 Correct 112 ms 21792 KB Output is correct
7 Correct 117 ms 21868 KB Output is correct
8 Correct 152 ms 24064 KB Output is correct
9 Correct 107 ms 23256 KB Output is correct
10 Correct 95 ms 18892 KB Output is correct
11 Correct 104 ms 20320 KB Output is correct
12 Correct 119 ms 20888 KB Output is correct
13 Correct 121 ms 26780 KB Output is correct
14 Correct 129 ms 26016 KB Output is correct
15 Correct 89 ms 20272 KB Output is correct
16 Correct 66 ms 20140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 139 ms 22492 KB Output is correct
3 Correct 128 ms 22616 KB Output is correct
4 Correct 133 ms 26520 KB Output is correct
5 Correct 139 ms 26712 KB Output is correct
6 Correct 136 ms 22492 KB Output is correct
7 Correct 99 ms 20324 KB Output is correct
8 Correct 90 ms 20312 KB Output is correct
9 Correct 64 ms 20016 KB Output is correct
10 Correct 95 ms 20156 KB Output is correct
11 Correct 111 ms 23440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 122 ms 20228 KB Output is correct
3 Correct 120 ms 20716 KB Output is correct
4 Correct 143 ms 21564 KB Output is correct
5 Correct 129 ms 23152 KB Output is correct
6 Correct 124 ms 20676 KB Output is correct
7 Correct 94 ms 20540 KB Output is correct
8 Correct 82 ms 21480 KB Output is correct
9 Correct 74 ms 11088 KB Output is correct
10 Correct 125 ms 23900 KB Output is correct
11 Correct 122 ms 23324 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 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 98 ms 20036 KB Output is correct
18 Correct 131 ms 25768 KB Output is correct
19 Correct 116 ms 21108 KB Output is correct
20 Correct 112 ms 21792 KB Output is correct
21 Correct 117 ms 21868 KB Output is correct
22 Correct 152 ms 24064 KB Output is correct
23 Correct 107 ms 23256 KB Output is correct
24 Correct 95 ms 18892 KB Output is correct
25 Correct 104 ms 20320 KB Output is correct
26 Correct 119 ms 20888 KB Output is correct
27 Correct 121 ms 26780 KB Output is correct
28 Correct 129 ms 26016 KB Output is correct
29 Correct 89 ms 20272 KB Output is correct
30 Correct 66 ms 20140 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 139 ms 22492 KB Output is correct
33 Correct 128 ms 22616 KB Output is correct
34 Correct 133 ms 26520 KB Output is correct
35 Correct 139 ms 26712 KB Output is correct
36 Correct 136 ms 22492 KB Output is correct
37 Correct 99 ms 20324 KB Output is correct
38 Correct 90 ms 20312 KB Output is correct
39 Correct 64 ms 20016 KB Output is correct
40 Correct 95 ms 20156 KB Output is correct
41 Correct 111 ms 23440 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 122 ms 20228 KB Output is correct
44 Correct 120 ms 20716 KB Output is correct
45 Correct 143 ms 21564 KB Output is correct
46 Correct 129 ms 23152 KB Output is correct
47 Correct 124 ms 20676 KB Output is correct
48 Correct 94 ms 20540 KB Output is correct
49 Correct 82 ms 21480 KB Output is correct
50 Correct 74 ms 11088 KB Output is correct
51 Correct 125 ms 23900 KB Output is correct
52 Correct 122 ms 23324 KB Output is correct
53 Correct 1 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 212 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 1 ms 468 KB Output is correct
58 Correct 1 ms 468 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 1 ms 468 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 95 ms 20056 KB Output is correct
68 Correct 119 ms 25780 KB Output is correct
69 Correct 108 ms 21064 KB Output is correct
70 Correct 121 ms 21872 KB Output is correct
71 Correct 111 ms 21820 KB Output is correct
72 Correct 139 ms 24016 KB Output is correct
73 Correct 94 ms 23236 KB Output is correct
74 Correct 83 ms 18928 KB Output is correct
75 Correct 90 ms 20344 KB Output is correct
76 Correct 115 ms 20780 KB Output is correct
77 Correct 131 ms 26796 KB Output is correct
78 Correct 121 ms 25964 KB Output is correct
79 Correct 90 ms 20284 KB Output is correct
80 Correct 65 ms 20040 KB Output is correct
81 Correct 130 ms 22404 KB Output is correct
82 Correct 164 ms 22588 KB Output is correct
83 Correct 138 ms 26556 KB Output is correct
84 Correct 122 ms 26708 KB Output is correct
85 Correct 130 ms 22556 KB Output is correct
86 Correct 99 ms 20268 KB Output is correct
87 Correct 92 ms 20284 KB Output is correct
88 Correct 87 ms 19904 KB Output is correct
89 Correct 120 ms 23416 KB Output is correct
90 Correct 131 ms 20180 KB Output is correct
91 Correct 120 ms 20820 KB Output is correct
92 Correct 132 ms 21584 KB Output is correct
93 Correct 146 ms 23148 KB Output is correct
94 Correct 131 ms 20676 KB Output is correct
95 Correct 99 ms 20548 KB Output is correct
96 Correct 84 ms 21544 KB Output is correct
97 Correct 75 ms 11052 KB Output is correct
98 Correct 128 ms 23996 KB Output is correct
99 Correct 121 ms 23332 KB Output is correct
100 Correct 67 ms 2756 KB Output is correct
101 Correct 151 ms 21008 KB Output is correct
102 Correct 110 ms 17152 KB Output is correct
103 Correct 127 ms 16748 KB Output is correct
104 Correct 130 ms 19608 KB Output is correct