Submission #748505

# Submission time Handle Problem Language Result Execution time Memory
748505 2023-05-26T11:45:36 Z inventiontime Stranded Far From Home (BOI22_island) C++17
100 / 100
464 ms 80932 KB
#include <bits/stdc++.h>
using namespace std;

#define int ll
#define endl '\n' //comment for interactive
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define pb push_back
#define re resize
#define ff first
#define ss second

#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)

#define print(x) cout << #x << ": " << x << endl << flush

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef vector<vi> vvi;
typedef priority_queue<int> pq;

template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }

const int inf = 1e17;

struct ufds {

    vi par;
    vector<set<int>> st; 
    ufds(vi a) : par(a), st(par.size()) {
        for(int i = 0; i < par.size(); i++) {
            par[i] = -par[i];
            st[i].insert(i);
        }
    };

    int find(int u) {
        if(par[u] < 0) return u;
        return par[u] = find(par[u]);
    }

    int size(int u) {
        return -par[find(u)];
    }

    bool merge(int u, int v) {
        u = find(u);
        v = find(v);
        if(u == v) return false;

        if(st[u].size() < st[v].size()) swap(u, v);
        for(auto x : st[v])
            st[u].insert(x);
        par[u] += par[v];
        par[v] = u;

        return true;
    }

    set<int>& setof(int u) {
        return st[find(u)];
    }

};

void solve() {

    int n, m;
    cin >> n >> m;
    vi s(n+1);
    vii ss(n+1);
    vi adj[n+1];
    loop1(i, n)
        cin >> s[i];
    loop1(i, n)
        ss[i] = {s[i], i};
    sort(all1(ss));
    loop1(i, m) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    
    vb incl(n+1);
    ufds ufds(s);
    vb valid(n+1, true);
    loop1(i, n) {
        auto [su, u] = ss[i];
        incl[u] = true;
        for(auto v : adj[u]) if(incl[v]) {
            if(ufds.find(u) != ufds.find(v)) {
                if(ufds.size(v) < ufds.size(u)) {
                    set<int>& st = ufds.setof(v);
                    for(auto x : st) valid[x] = false;
                    st.clear();
                }
            }
        }
        for(auto v : adj[u]) if(incl[v])
            ufds.merge(u, v);
    }

    loop1(i, n) cout << valid[i];
    cout << endl;

}

signed main() {

    fast_io;

    int t = 1; //cin >> t;
    while(t--)
        solve();

    return 0;

}

Compilation message

island.cpp: In constructor 'ufds::ufds(vi)':
island.cpp:40:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |         for(int i = 0; i < par.size(); i++) {
      |                        ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 199 ms 43372 KB Output is correct
4 Correct 209 ms 50112 KB Output is correct
5 Correct 286 ms 56264 KB Output is correct
6 Correct 289 ms 59544 KB Output is correct
7 Correct 297 ms 58824 KB Output is correct
8 Correct 203 ms 51376 KB Output is correct
9 Correct 231 ms 68500 KB Output is correct
10 Correct 166 ms 43100 KB Output is correct
11 Correct 214 ms 50992 KB Output is correct
12 Correct 237 ms 48176 KB Output is correct
13 Correct 188 ms 48940 KB Output is correct
14 Correct 189 ms 49064 KB Output is correct
15 Correct 219 ms 50452 KB Output is correct
16 Correct 151 ms 49472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 338 ms 60648 KB Output is correct
3 Correct 337 ms 65092 KB Output is correct
4 Correct 213 ms 50408 KB Output is correct
5 Correct 182 ms 48816 KB Output is correct
6 Correct 321 ms 64580 KB Output is correct
7 Correct 279 ms 50352 KB Output is correct
8 Correct 215 ms 50376 KB Output is correct
9 Correct 142 ms 49216 KB Output is correct
10 Correct 183 ms 41648 KB Output is correct
11 Correct 210 ms 47420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 311 ms 47660 KB Output is correct
3 Correct 351 ms 55116 KB Output is correct
4 Correct 360 ms 63304 KB Output is correct
5 Correct 464 ms 76708 KB Output is correct
6 Correct 338 ms 61788 KB Output is correct
7 Correct 195 ms 52092 KB Output is correct
8 Correct 143 ms 46128 KB Output is correct
9 Correct 176 ms 26568 KB Output is correct
10 Correct 431 ms 80932 KB Output is correct
11 Correct 206 ms 47492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 2 ms 724 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 199 ms 43372 KB Output is correct
18 Correct 209 ms 50112 KB Output is correct
19 Correct 286 ms 56264 KB Output is correct
20 Correct 289 ms 59544 KB Output is correct
21 Correct 297 ms 58824 KB Output is correct
22 Correct 203 ms 51376 KB Output is correct
23 Correct 231 ms 68500 KB Output is correct
24 Correct 166 ms 43100 KB Output is correct
25 Correct 214 ms 50992 KB Output is correct
26 Correct 237 ms 48176 KB Output is correct
27 Correct 188 ms 48940 KB Output is correct
28 Correct 189 ms 49064 KB Output is correct
29 Correct 219 ms 50452 KB Output is correct
30 Correct 151 ms 49472 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 338 ms 60648 KB Output is correct
33 Correct 337 ms 65092 KB Output is correct
34 Correct 213 ms 50408 KB Output is correct
35 Correct 182 ms 48816 KB Output is correct
36 Correct 321 ms 64580 KB Output is correct
37 Correct 279 ms 50352 KB Output is correct
38 Correct 215 ms 50376 KB Output is correct
39 Correct 142 ms 49216 KB Output is correct
40 Correct 183 ms 41648 KB Output is correct
41 Correct 210 ms 47420 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 311 ms 47660 KB Output is correct
44 Correct 351 ms 55116 KB Output is correct
45 Correct 360 ms 63304 KB Output is correct
46 Correct 464 ms 76708 KB Output is correct
47 Correct 338 ms 61788 KB Output is correct
48 Correct 195 ms 52092 KB Output is correct
49 Correct 143 ms 46128 KB Output is correct
50 Correct 176 ms 26568 KB Output is correct
51 Correct 431 ms 80932 KB Output is correct
52 Correct 206 ms 47492 KB Output is correct
53 Correct 0 ms 324 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 1 ms 320 KB Output is correct
56 Correct 2 ms 596 KB Output is correct
57 Correct 3 ms 724 KB Output is correct
58 Correct 2 ms 724 KB Output is correct
59 Correct 2 ms 592 KB Output is correct
60 Correct 2 ms 596 KB Output is correct
61 Correct 2 ms 724 KB Output is correct
62 Correct 2 ms 848 KB Output is correct
63 Correct 2 ms 724 KB Output is correct
64 Correct 2 ms 852 KB Output is correct
65 Correct 2 ms 724 KB Output is correct
66 Correct 3 ms 716 KB Output is correct
67 Correct 178 ms 47816 KB Output is correct
68 Correct 204 ms 50052 KB Output is correct
69 Correct 299 ms 56276 KB Output is correct
70 Correct 307 ms 59464 KB Output is correct
71 Correct 274 ms 58816 KB Output is correct
72 Correct 250 ms 51368 KB Output is correct
73 Correct 262 ms 68460 KB Output is correct
74 Correct 156 ms 42956 KB Output is correct
75 Correct 200 ms 50944 KB Output is correct
76 Correct 238 ms 48144 KB Output is correct
77 Correct 193 ms 48856 KB Output is correct
78 Correct 182 ms 48944 KB Output is correct
79 Correct 205 ms 50384 KB Output is correct
80 Correct 144 ms 49124 KB Output is correct
81 Correct 342 ms 64988 KB Output is correct
82 Correct 365 ms 65108 KB Output is correct
83 Correct 244 ms 50372 KB Output is correct
84 Correct 207 ms 48776 KB Output is correct
85 Correct 351 ms 64524 KB Output is correct
86 Correct 236 ms 50368 KB Output is correct
87 Correct 221 ms 50464 KB Output is correct
88 Correct 189 ms 41704 KB Output is correct
89 Correct 201 ms 47396 KB Output is correct
90 Correct 338 ms 52132 KB Output is correct
91 Correct 321 ms 55224 KB Output is correct
92 Correct 371 ms 63280 KB Output is correct
93 Correct 425 ms 76716 KB Output is correct
94 Correct 356 ms 61780 KB Output is correct
95 Correct 191 ms 51900 KB Output is correct
96 Correct 147 ms 46128 KB Output is correct
97 Correct 186 ms 26624 KB Output is correct
98 Correct 389 ms 80928 KB Output is correct
99 Correct 195 ms 47476 KB Output is correct
100 Correct 44 ms 7056 KB Output is correct
101 Correct 385 ms 56340 KB Output is correct
102 Correct 254 ms 47456 KB Output is correct
103 Correct 283 ms 40368 KB Output is correct
104 Correct 303 ms 48408 KB Output is correct