Submission #710763

# Submission time Handle Problem Language Result Execution time Memory
710763 2023-03-15T18:06:46 Z sysia Stranded Far From Home (BOI22_island) C++17
100 / 100
748 ms 67332 KB
//Sylwia Sapkowska
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
const int oo = 1e18, oo2 = 1e9+7, K = 30;
const int mod = 998244353;

set<T>s;

struct DSU{
    vector<int>rep, sz;
    vector<bool>ans;
    vector<vector<int>>tab;

    void clear(int v){
        v = f(v);
        debug(v, tab[v]);
        for (auto x: tab[v]){
            ans[x] = 0;
        }
        tab[v].clear();
    }

    DSU(int n, vector<int>&a){
        rep.assign(n+1, 0);
        tab.resize(n+1);
        ans.assign(n+1, 1);
        for (int i = 1; i<=n; i++) tab[i].emplace_back(i);
        iota(rep.begin(), rep.end(), 0);
        sz = a;
    }

    int f(int a){return a == rep[a] ? a : rep[a] = f(rep[a]);}

    bool u(int a, int b){
        a = f(a);b = f(b);
        if (a == b) return 0;
        if ((int)tab[a].size() < (int)tab[b].size()) swap(a, b);
        s.erase({sz[a], a});
        s.erase({sz[b], b});
        for (auto x: tab[b]) tab[a].emplace_back(x);
        tab[b].clear();
        sz[a] += sz[b];
        rep[b] = a;
        s.insert({sz[a], a});
        return 1;
    }

};

void solve(){
    int n, m; cin >> n >> m;
    vector<int>a(n+1);
    map<int, vector<int>>event;
    for (int i = 1; i<=n; i++) {
        cin >> a[i];
        event[a[i]].emplace_back(i);
    }
    vector<T>edges;
    vector<vector<int>>g(n+1);
    for (int i = 0; i<m; i++) {
        int a, b; cin >> a >> b;
        edges.emplace_back(a, b);
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    sort(edges.begin(), edges.end(), [&](auto x, auto y){
        return max(a[x.first], a[x.second]) < max(a[y.first], a[y.second]);
    });
    DSU dsu(n+2, a);
    int ptr = -1;
    vector<int>b = a;
    sort(b.begin(), b.end());
    for (auto [w, curr]: event){
        for (auto v: curr) s.insert({dsu.sz[v], v});
        while (ptr+1 < (int)edges.size() && max(a[edges[ptr+1].first], a[edges[ptr+1].second]) <= w) {
            ptr++;
            debug(edges[ptr]);
            dsu.u(edges[ptr].first, edges[ptr].second);
        }
        debug(w);
        if (b.back() == w) break;
        int nxt = b[upper_bound(b.begin(), b.end(), w) - b.begin()];
        debug(w, nxt);
        while ((int)s.size() && s.begin()->first < nxt){
            int v = s.begin()->second;
            debug(v);
            s.erase(s.begin());
            dsu.clear(v);
        }
    }
    for (int i = 1; i<=n; i++) cout << (dsu.ans[i] ? 1 : 0);
    cout << "\n";
}

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

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 4 ms 852 KB Output is correct
5 Correct 4 ms 840 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 716 KB Output is correct
9 Correct 3 ms 728 KB Output is correct
10 Correct 4 ms 852 KB Output is correct
11 Correct 4 ms 852 KB Output is correct
12 Correct 3 ms 852 KB Output is correct
13 Correct 3 ms 852 KB Output is correct
14 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 326 ms 58148 KB Output is correct
4 Correct 582 ms 58756 KB Output is correct
5 Correct 501 ms 61072 KB Output is correct
6 Correct 431 ms 58316 KB Output is correct
7 Correct 412 ms 63308 KB Output is correct
8 Correct 732 ms 57140 KB Output is correct
9 Correct 328 ms 61568 KB Output is correct
10 Correct 252 ms 51232 KB Output is correct
11 Correct 457 ms 53360 KB Output is correct
12 Correct 748 ms 48120 KB Output is correct
13 Correct 654 ms 59516 KB Output is correct
14 Correct 626 ms 57024 KB Output is correct
15 Correct 282 ms 58072 KB Output is correct
16 Correct 222 ms 57548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 684 ms 63248 KB Output is correct
3 Correct 612 ms 62736 KB Output is correct
4 Correct 686 ms 62836 KB Output is correct
5 Correct 625 ms 61808 KB Output is correct
6 Correct 646 ms 67316 KB Output is correct
7 Correct 349 ms 61868 KB Output is correct
8 Correct 295 ms 61768 KB Output is correct
9 Correct 211 ms 60556 KB Output is correct
10 Correct 558 ms 59824 KB Output is correct
11 Correct 523 ms 50224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 494 ms 40848 KB Output is correct
3 Correct 524 ms 45436 KB Output is correct
4 Correct 486 ms 48000 KB Output is correct
5 Correct 623 ms 59172 KB Output is correct
6 Correct 614 ms 54844 KB Output is correct
7 Correct 436 ms 56988 KB Output is correct
8 Correct 223 ms 50840 KB Output is correct
9 Correct 200 ms 26452 KB Output is correct
10 Correct 652 ms 59120 KB Output is correct
11 Correct 486 ms 49392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 4 ms 852 KB Output is correct
5 Correct 4 ms 840 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 716 KB Output is correct
9 Correct 3 ms 728 KB Output is correct
10 Correct 4 ms 852 KB Output is correct
11 Correct 4 ms 852 KB Output is correct
12 Correct 3 ms 852 KB Output is correct
13 Correct 3 ms 852 KB Output is correct
14 Correct 3 ms 724 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 326 ms 58148 KB Output is correct
18 Correct 582 ms 58756 KB Output is correct
19 Correct 501 ms 61072 KB Output is correct
20 Correct 431 ms 58316 KB Output is correct
21 Correct 412 ms 63308 KB Output is correct
22 Correct 732 ms 57140 KB Output is correct
23 Correct 328 ms 61568 KB Output is correct
24 Correct 252 ms 51232 KB Output is correct
25 Correct 457 ms 53360 KB Output is correct
26 Correct 748 ms 48120 KB Output is correct
27 Correct 654 ms 59516 KB Output is correct
28 Correct 626 ms 57024 KB Output is correct
29 Correct 282 ms 58072 KB Output is correct
30 Correct 222 ms 57548 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 684 ms 63248 KB Output is correct
33 Correct 612 ms 62736 KB Output is correct
34 Correct 686 ms 62836 KB Output is correct
35 Correct 625 ms 61808 KB Output is correct
36 Correct 646 ms 67316 KB Output is correct
37 Correct 349 ms 61868 KB Output is correct
38 Correct 295 ms 61768 KB Output is correct
39 Correct 211 ms 60556 KB Output is correct
40 Correct 558 ms 59824 KB Output is correct
41 Correct 523 ms 50224 KB Output is correct
42 Correct 1 ms 316 KB Output is correct
43 Correct 494 ms 40848 KB Output is correct
44 Correct 524 ms 45436 KB Output is correct
45 Correct 486 ms 48000 KB Output is correct
46 Correct 623 ms 59172 KB Output is correct
47 Correct 614 ms 54844 KB Output is correct
48 Correct 436 ms 56988 KB Output is correct
49 Correct 223 ms 50840 KB Output is correct
50 Correct 200 ms 26452 KB Output is correct
51 Correct 652 ms 59120 KB Output is correct
52 Correct 486 ms 49392 KB Output is correct
53 Correct 1 ms 316 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Correct 1 ms 320 KB Output is correct
56 Correct 3 ms 852 KB Output is correct
57 Correct 3 ms 836 KB Output is correct
58 Correct 2 ms 712 KB Output is correct
59 Correct 3 ms 724 KB Output is correct
60 Correct 3 ms 724 KB Output is correct
61 Correct 3 ms 724 KB Output is correct
62 Correct 3 ms 840 KB Output is correct
63 Correct 3 ms 852 KB Output is correct
64 Correct 3 ms 852 KB Output is correct
65 Correct 4 ms 852 KB Output is correct
66 Correct 4 ms 724 KB Output is correct
67 Correct 283 ms 62176 KB Output is correct
68 Correct 543 ms 61324 KB Output is correct
69 Correct 396 ms 64812 KB Output is correct
70 Correct 379 ms 62008 KB Output is correct
71 Correct 388 ms 66884 KB Output is correct
72 Correct 677 ms 60704 KB Output is correct
73 Correct 332 ms 65276 KB Output is correct
74 Correct 209 ms 54000 KB Output is correct
75 Correct 419 ms 56128 KB Output is correct
76 Correct 593 ms 50376 KB Output is correct
77 Correct 566 ms 61816 KB Output is correct
78 Correct 552 ms 59324 KB Output is correct
79 Correct 257 ms 61840 KB Output is correct
80 Correct 206 ms 60524 KB Output is correct
81 Correct 545 ms 67180 KB Output is correct
82 Correct 531 ms 62824 KB Output is correct
83 Correct 623 ms 63008 KB Output is correct
84 Correct 589 ms 61840 KB Output is correct
85 Correct 582 ms 67332 KB Output is correct
86 Correct 260 ms 61900 KB Output is correct
87 Correct 267 ms 61816 KB Output is correct
88 Correct 489 ms 59820 KB Output is correct
89 Correct 490 ms 50176 KB Output is correct
90 Correct 394 ms 44828 KB Output is correct
91 Correct 394 ms 45604 KB Output is correct
92 Correct 450 ms 47968 KB Output is correct
93 Correct 681 ms 59136 KB Output is correct
94 Correct 610 ms 54788 KB Output is correct
95 Correct 436 ms 57136 KB Output is correct
96 Correct 243 ms 50824 KB Output is correct
97 Correct 208 ms 26412 KB Output is correct
98 Correct 687 ms 59160 KB Output is correct
99 Correct 445 ms 49496 KB Output is correct
100 Correct 66 ms 10476 KB Output is correct
101 Correct 554 ms 60236 KB Output is correct
102 Correct 497 ms 46904 KB Output is correct
103 Correct 383 ms 49512 KB Output is correct
104 Correct 616 ms 62044 KB Output is correct