Submission #694082

# Submission time Handle Problem Language Result Execution time Memory
694082 2023-02-03T17:46:26 Z _martynas Stranded Far From Home (BOI22_island) C++11
100 / 100
359 ms 37668 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pli = pair<ll, int>;
using vi = vector<int>;
using vl = vector<ll>;

#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const int MXN = 2e5+5;

int n, m;
ll A[MXN];
vi adj[MXN];
vi res_adj[MXN]; // directional graph describes how I propagate results

struct DSU {
    int n;
    vi par;
    vi sz;
    vi mx_id;
    vl pop;
    void init(int _n) {
        n = _n;
        par = vi(n+1);
        sz = vi(n+1, 1);
        mx_id = vi(n+1);
        pop = vl(n+1);
        iota(par.begin()+1, par.begin()+n+1, 1);
        iota(mx_id.begin()+1, mx_id.begin()+n+1, 1);
        for(int i = 1; i <= n; i++) pop[i] = A[i];
    }
    int find_set(int u) { return par[u] == u ? u : par[u] = find_set(par[u]); }
    void unite(int u, int v) {
        u = find_set(u), v = find_set(v);
        if(u == v) return;
        int new_mx_id = A[mx_id[u]] >= A[mx_id[v]] ? mx_id[u] : mx_id[v];
        if(sz[u] < sz[v]) swap(u, v);
        mx_id[u] = new_mx_id;
        par[v] = u;
        sz[u] += sz[v];
        pop[u] += pop[v];
    }
};

int main() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> A[i];
    for(int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        adj[u].PB(v), adj[v].PB(u);
    }
    string ans(n+1, '0');
    ll mx = *max_element(A+1, A+n+1);
    vector<pli> B;
    for(int i = 1; i <= n; i++) {
        if(A[i] == mx) ans[i] = '1';
        B.PB({A[i], i});
    }
    sort(all(B));
    DSU dsu;
    dsu.init(n);
    for(auto p : B) {
        int u = p.S;
        for(int v : adj[u]) {
            if(A[v] <= A[u]) {
                if(dsu.pop[dsu.find_set(v)] >= A[u]) {
                    res_adj[u].PB(dsu.mx_id[dsu.find_set(v)]);
                }
            }
        }
        for(int v : adj[u]) {
            if(A[v] <= A[u]) {
                dsu.unite(u, v);
            }
        }
    }
    queue<int> Q;
    for(int i = 1; i <= n; i++) if(ans[i] == '1') Q.push(i);
    while(!Q.empty()) {
        int i = Q.front();
        Q.pop();
        for(int j : res_adj[i]) {
            if(ans[j] == '0') {
                ans[j] = '1';
                Q.push(j);
            }
        }
    }
    cout << ans.substr(1) << "\n";
    return 0;
}

/*
4 4
2 2 4 3
1 2
1 3
2 3
3 4
ans: 1110
4 3
4 2 2 1
1 2
3 2
4 1
ans: 1110

5 4
1 3 1 2 7
1 2
2 3
3 4
4 5

*/
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9880 KB Output is correct
6 Correct 8 ms 9844 KB Output is correct
7 Correct 8 ms 9904 KB Output is correct
8 Correct 6 ms 9848 KB Output is correct
9 Correct 8 ms 9940 KB Output is correct
10 Correct 7 ms 9844 KB Output is correct
11 Correct 9 ms 9812 KB Output is correct
12 Correct 8 ms 9848 KB Output is correct
13 Correct 8 ms 9844 KB Output is correct
14 Correct 7 ms 9808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 267 ms 28600 KB Output is correct
4 Correct 253 ms 31484 KB Output is correct
5 Correct 269 ms 27436 KB Output is correct
6 Correct 290 ms 27800 KB Output is correct
7 Correct 277 ms 27888 KB Output is correct
8 Correct 305 ms 31436 KB Output is correct
9 Correct 266 ms 26548 KB Output is correct
10 Correct 204 ms 25900 KB Output is correct
11 Correct 259 ms 33560 KB Output is correct
12 Correct 257 ms 31268 KB Output is correct
13 Correct 243 ms 31388 KB Output is correct
14 Correct 231 ms 31464 KB Output is correct
15 Correct 272 ms 31364 KB Output is correct
16 Correct 202 ms 31188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 311 ms 27500 KB Output is correct
3 Correct 308 ms 27528 KB Output is correct
4 Correct 322 ms 36456 KB Output is correct
5 Correct 232 ms 34368 KB Output is correct
6 Correct 323 ms 31968 KB Output is correct
7 Correct 306 ms 35820 KB Output is correct
8 Correct 284 ms 35768 KB Output is correct
9 Correct 204 ms 34868 KB Output is correct
10 Correct 246 ms 31980 KB Output is correct
11 Correct 254 ms 32052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 330 ms 28372 KB Output is correct
3 Correct 349 ms 32824 KB Output is correct
4 Correct 315 ms 33168 KB Output is correct
5 Correct 344 ms 36456 KB Output is correct
6 Correct 285 ms 35340 KB Output is correct
7 Correct 309 ms 37668 KB Output is correct
8 Correct 201 ms 34640 KB Output is correct
9 Correct 233 ms 23992 KB Output is correct
10 Correct 283 ms 34360 KB Output is correct
11 Correct 263 ms 32096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9696 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9880 KB Output is correct
6 Correct 8 ms 9844 KB Output is correct
7 Correct 8 ms 9904 KB Output is correct
8 Correct 6 ms 9848 KB Output is correct
9 Correct 8 ms 9940 KB Output is correct
10 Correct 7 ms 9844 KB Output is correct
11 Correct 9 ms 9812 KB Output is correct
12 Correct 8 ms 9848 KB Output is correct
13 Correct 8 ms 9844 KB Output is correct
14 Correct 7 ms 9808 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 267 ms 28600 KB Output is correct
18 Correct 253 ms 31484 KB Output is correct
19 Correct 269 ms 27436 KB Output is correct
20 Correct 290 ms 27800 KB Output is correct
21 Correct 277 ms 27888 KB Output is correct
22 Correct 305 ms 31436 KB Output is correct
23 Correct 266 ms 26548 KB Output is correct
24 Correct 204 ms 25900 KB Output is correct
25 Correct 259 ms 33560 KB Output is correct
26 Correct 257 ms 31268 KB Output is correct
27 Correct 243 ms 31388 KB Output is correct
28 Correct 231 ms 31464 KB Output is correct
29 Correct 272 ms 31364 KB Output is correct
30 Correct 202 ms 31188 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 311 ms 27500 KB Output is correct
33 Correct 308 ms 27528 KB Output is correct
34 Correct 322 ms 36456 KB Output is correct
35 Correct 232 ms 34368 KB Output is correct
36 Correct 323 ms 31968 KB Output is correct
37 Correct 306 ms 35820 KB Output is correct
38 Correct 284 ms 35768 KB Output is correct
39 Correct 204 ms 34868 KB Output is correct
40 Correct 246 ms 31980 KB Output is correct
41 Correct 254 ms 32052 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 330 ms 28372 KB Output is correct
44 Correct 349 ms 32824 KB Output is correct
45 Correct 315 ms 33168 KB Output is correct
46 Correct 344 ms 36456 KB Output is correct
47 Correct 285 ms 35340 KB Output is correct
48 Correct 309 ms 37668 KB Output is correct
49 Correct 201 ms 34640 KB Output is correct
50 Correct 233 ms 23992 KB Output is correct
51 Correct 283 ms 34360 KB Output is correct
52 Correct 263 ms 32096 KB Output is correct
53 Correct 8 ms 9700 KB Output is correct
54 Correct 5 ms 9684 KB Output is correct
55 Correct 5 ms 9704 KB Output is correct
56 Correct 9 ms 9812 KB Output is correct
57 Correct 11 ms 9844 KB Output is correct
58 Correct 7 ms 9872 KB Output is correct
59 Correct 7 ms 9840 KB Output is correct
60 Correct 7 ms 9844 KB Output is correct
61 Correct 7 ms 9940 KB Output is correct
62 Correct 9 ms 9896 KB Output is correct
63 Correct 8 ms 9812 KB Output is correct
64 Correct 8 ms 9840 KB Output is correct
65 Correct 6 ms 9840 KB Output is correct
66 Correct 6 ms 9848 KB Output is correct
67 Correct 308 ms 33036 KB Output is correct
68 Correct 235 ms 34548 KB Output is correct
69 Correct 284 ms 31540 KB Output is correct
70 Correct 304 ms 32204 KB Output is correct
71 Correct 277 ms 32192 KB Output is correct
72 Correct 308 ms 35908 KB Output is correct
73 Correct 260 ms 30976 KB Output is correct
74 Correct 214 ms 29552 KB Output is correct
75 Correct 237 ms 36976 KB Output is correct
76 Correct 291 ms 34364 KB Output is correct
77 Correct 249 ms 34220 KB Output is correct
78 Correct 236 ms 34536 KB Output is correct
79 Correct 359 ms 35816 KB Output is correct
80 Correct 200 ms 35100 KB Output is correct
81 Correct 308 ms 32100 KB Output is correct
82 Correct 309 ms 31968 KB Output is correct
83 Correct 295 ms 36460 KB Output is correct
84 Correct 292 ms 34320 KB Output is correct
85 Correct 319 ms 32048 KB Output is correct
86 Correct 307 ms 35808 KB Output is correct
87 Correct 280 ms 35824 KB Output is correct
88 Correct 244 ms 32096 KB Output is correct
89 Correct 253 ms 32008 KB Output is correct
90 Correct 320 ms 32808 KB Output is correct
91 Correct 305 ms 32812 KB Output is correct
92 Correct 346 ms 33072 KB Output is correct
93 Correct 332 ms 36536 KB Output is correct
94 Correct 307 ms 35276 KB Output is correct
95 Correct 319 ms 37548 KB Output is correct
96 Correct 192 ms 34644 KB Output is correct
97 Correct 188 ms 24048 KB Output is correct
98 Correct 271 ms 34464 KB Output is correct
99 Correct 237 ms 32096 KB Output is correct
100 Correct 106 ms 14956 KB Output is correct
101 Correct 336 ms 32172 KB Output is correct
102 Correct 277 ms 31468 KB Output is correct
103 Correct 303 ms 29480 KB Output is correct
104 Correct 317 ms 31856 KB Output is correct