Submission #674187

# Submission time Handle Problem Language Result Execution time Memory
674187 2022-12-23T12:06:38 Z stanislavpolyn Stranded Far From Home (BOI22_island) C++17
100 / 100
332 ms 67008 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define pw(x) (1LL << (x))
#define sz(x) (int)(x).size()

using namespace std;

mt19937_64 rng(228);

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T>
bool umx(T& a, T b) { return a < b ? a = b, 1 : 0; }

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int N = 4e5 + 5;

int n, m;
int a[N];
ve<int> g[N];
ve<pii> u;

ve<int> v[N];
int link[N];
ll sum[N];

int root(int x) {
    if (link[x] == x) return x;
    else return root(link[x]);
}

ve<int> G[N];
int nVer;
int who[N];
bool badCmp[N];

void uni(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) {
        return;
    }
    if (sz(v[a]) > sz(v[b])) swap(a, b);

    sum[b] += sum[a];
    link[a] = b;
    fe (x, v[a]) v[b].pb(x);
    ++nVer;
//    cout << "add " << nVer << " " << who[a] << "\n";
//    cout << "add " << nVer << " " << who[b] << "\n";
    G[nVer].pb(who[a]);
    G[nVer].pb(who[b]);
    who[b] = nVer;
}

bool bad[N];

bool brute(int v) {
    ll sum = 0;
    set<pii> q;
    ve<bool> vis(n + 1);
    sum = a[v];
    vis[v] = 1;

//    cout << "GO " << v << " " << sum << "\n";
    fe (to, g[v]) {
        q.insert({a[to], to});
        vis[to] = 1;
    }
    while (sz(q)) {
//        cout << "FROM " << v << "\n";

        int v = q.begin()->se;
        q.erase(q.begin());

//        cout << "GO " << v << " " << a[v] << "\n";

        if (a[v] > sum) {
            return 0;
        }

        sum += a[v];
        fe (to, g[v]) {
            if (!vis[to]) {
                vis[to] = 1;
                q.insert({a[to], to});
            }
        }
    }
    return 1;
}

int rnd(int l, int r) {
    return rng() % (r - l + 1) + l;
}

void dfs(int v = nVer, bool bad = 0) {
    bad |= badCmp[v];

    if (v <= n) {
        ::bad[v] = bad;
    }

    fe (to, G[v]) {
        dfs(to, bad);
    }
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    cin >> n >> m;

    nVer = n;

    fr (i, 1, n) {
        cin >> a[i];
//        a[i] = rnd(1, 20);
//        cout << a[i] << " ";
        u.pb({a[i], i});
    }
//    cout << "\n";

//    assert(m == n - 1);
    fr (i, 1, m) {
        int a, b;
        cin >> a >> b;
//        a = i;
//        b = i + 1;


        g[a].pb(b);
        g[b].pb(a);
    }

    sort(all(u));

//    fe (x, a) {
//        cout << x.fi << " " << x.se << "\n";
//    }

    fr (i, 1, n) {
        link[i] = i;
        who[i] = i;
        sum[i] = a[i];
        v[i] = {i};
    }

    fr (i, 0, sz(u) - 1) {
        int ptr = i;
        ve<int> ver;
        while (ptr < sz(u) && u[ptr].fi == u[i].fi) {
            ver.pb(u[ptr].se);
            fe (to, g[u[ptr].se]) {
                if (a[to] <= u[i].fi) {
                    uni(u[ptr].se, to);
                }
            }
            ptr++;
        }

//        cout << "added <= " << u[ptr - 1].fi << "\n";
//        cout << "other >= " << u[ptr].fi << "\n\n";

        if (ptr < sz(u)) {
            int ptr2 = ptr;
            while (ptr2 < sz(u) && u[ptr2].fi == u[ptr].fi) {
                fe (to, g[u[ptr2].se]) {
                    if (a[to] < u[ptr].fi) {
                        int v = root(to);
//                        cout << "MARK " << who[v] << "\n";
//                        fe (cur, ::v[v]) cout << cur << " ";
//                        cout << "\n";

                        if (sum[v] < u[ptr].fi) {
                            badCmp[who[v]] = 1;
//                            fe (cur, ::v[v]) {
//                                bad[cur] = 1;
//                            }
                        }
                    }
                }
                ptr2++;
            }
        }

        i = ptr - 1;
    }

    dfs();
    fr (i, 1, n) {
        cout << !bad[i];
    }
    cout << "\n";

//    fr (i, 1, n) {
//        cout << brute(i);
//    }
//    cout << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 15 ms 28532 KB Output is correct
4 Correct 16 ms 28756 KB Output is correct
5 Correct 16 ms 28716 KB Output is correct
6 Correct 16 ms 28656 KB Output is correct
7 Correct 16 ms 28784 KB Output is correct
8 Correct 15 ms 28660 KB Output is correct
9 Correct 15 ms 28756 KB Output is correct
10 Correct 16 ms 28912 KB Output is correct
11 Correct 16 ms 28756 KB Output is correct
12 Correct 15 ms 28788 KB Output is correct
13 Correct 15 ms 28756 KB Output is correct
14 Correct 16 ms 28756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 28520 KB Output is correct
2 Correct 14 ms 28516 KB Output is correct
3 Correct 178 ms 56560 KB Output is correct
4 Correct 169 ms 58088 KB Output is correct
5 Correct 251 ms 57992 KB Output is correct
6 Correct 266 ms 59140 KB Output is correct
7 Correct 266 ms 59200 KB Output is correct
8 Correct 196 ms 58980 KB Output is correct
9 Correct 201 ms 62700 KB Output is correct
10 Correct 194 ms 59224 KB Output is correct
11 Correct 195 ms 59144 KB Output is correct
12 Correct 229 ms 56388 KB Output is correct
13 Correct 179 ms 58952 KB Output is correct
14 Correct 160 ms 57956 KB Output is correct
15 Correct 175 ms 58324 KB Output is correct
16 Correct 122 ms 57924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 315 ms 60804 KB Output is correct
3 Correct 318 ms 60740 KB Output is correct
4 Correct 189 ms 58904 KB Output is correct
5 Correct 158 ms 57892 KB Output is correct
6 Correct 309 ms 60828 KB Output is correct
7 Correct 200 ms 58208 KB Output is correct
8 Correct 194 ms 58188 KB Output is correct
9 Correct 141 ms 57872 KB Output is correct
10 Correct 181 ms 57648 KB Output is correct
11 Correct 207 ms 56272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28512 KB Output is correct
2 Correct 302 ms 56628 KB Output is correct
3 Correct 297 ms 57288 KB Output is correct
4 Correct 303 ms 58540 KB Output is correct
5 Correct 271 ms 58032 KB Output is correct
6 Correct 239 ms 55728 KB Output is correct
7 Correct 164 ms 60200 KB Output is correct
8 Correct 127 ms 58580 KB Output is correct
9 Correct 143 ms 46000 KB Output is correct
10 Correct 253 ms 62296 KB Output is correct
11 Correct 199 ms 58960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 14 ms 28500 KB Output is correct
3 Correct 15 ms 28532 KB Output is correct
4 Correct 16 ms 28756 KB Output is correct
5 Correct 16 ms 28716 KB Output is correct
6 Correct 16 ms 28656 KB Output is correct
7 Correct 16 ms 28784 KB Output is correct
8 Correct 15 ms 28660 KB Output is correct
9 Correct 15 ms 28756 KB Output is correct
10 Correct 16 ms 28912 KB Output is correct
11 Correct 16 ms 28756 KB Output is correct
12 Correct 15 ms 28788 KB Output is correct
13 Correct 15 ms 28756 KB Output is correct
14 Correct 16 ms 28756 KB Output is correct
15 Correct 14 ms 28520 KB Output is correct
16 Correct 14 ms 28516 KB Output is correct
17 Correct 178 ms 56560 KB Output is correct
18 Correct 169 ms 58088 KB Output is correct
19 Correct 251 ms 57992 KB Output is correct
20 Correct 266 ms 59140 KB Output is correct
21 Correct 266 ms 59200 KB Output is correct
22 Correct 196 ms 58980 KB Output is correct
23 Correct 201 ms 62700 KB Output is correct
24 Correct 194 ms 59224 KB Output is correct
25 Correct 195 ms 59144 KB Output is correct
26 Correct 229 ms 56388 KB Output is correct
27 Correct 179 ms 58952 KB Output is correct
28 Correct 160 ms 57956 KB Output is correct
29 Correct 175 ms 58324 KB Output is correct
30 Correct 122 ms 57924 KB Output is correct
31 Correct 15 ms 28500 KB Output is correct
32 Correct 315 ms 60804 KB Output is correct
33 Correct 318 ms 60740 KB Output is correct
34 Correct 189 ms 58904 KB Output is correct
35 Correct 158 ms 57892 KB Output is correct
36 Correct 309 ms 60828 KB Output is correct
37 Correct 200 ms 58208 KB Output is correct
38 Correct 194 ms 58188 KB Output is correct
39 Correct 141 ms 57872 KB Output is correct
40 Correct 181 ms 57648 KB Output is correct
41 Correct 207 ms 56272 KB Output is correct
42 Correct 15 ms 28512 KB Output is correct
43 Correct 302 ms 56628 KB Output is correct
44 Correct 297 ms 57288 KB Output is correct
45 Correct 303 ms 58540 KB Output is correct
46 Correct 271 ms 58032 KB Output is correct
47 Correct 239 ms 55728 KB Output is correct
48 Correct 164 ms 60200 KB Output is correct
49 Correct 127 ms 58580 KB Output is correct
50 Correct 143 ms 46000 KB Output is correct
51 Correct 253 ms 62296 KB Output is correct
52 Correct 199 ms 58960 KB Output is correct
53 Correct 14 ms 28500 KB Output is correct
54 Correct 14 ms 28516 KB Output is correct
55 Correct 14 ms 28500 KB Output is correct
56 Correct 17 ms 28708 KB Output is correct
57 Correct 15 ms 28884 KB Output is correct
58 Correct 15 ms 28756 KB Output is correct
59 Correct 16 ms 28680 KB Output is correct
60 Correct 15 ms 28716 KB Output is correct
61 Correct 15 ms 28788 KB Output is correct
62 Correct 15 ms 28884 KB Output is correct
63 Correct 16 ms 28792 KB Output is correct
64 Correct 18 ms 28808 KB Output is correct
65 Correct 16 ms 28876 KB Output is correct
66 Correct 16 ms 28792 KB Output is correct
67 Correct 179 ms 61028 KB Output is correct
68 Correct 164 ms 60916 KB Output is correct
69 Correct 260 ms 62148 KB Output is correct
70 Correct 268 ms 63340 KB Output is correct
71 Correct 271 ms 63360 KB Output is correct
72 Correct 211 ms 63492 KB Output is correct
73 Correct 201 ms 67008 KB Output is correct
74 Correct 200 ms 62604 KB Output is correct
75 Correct 204 ms 62480 KB Output is correct
76 Correct 236 ms 59452 KB Output is correct
77 Correct 170 ms 61820 KB Output is correct
78 Correct 167 ms 60944 KB Output is correct
79 Correct 174 ms 62692 KB Output is correct
80 Correct 130 ms 61500 KB Output is correct
81 Correct 319 ms 65128 KB Output is correct
82 Correct 314 ms 65088 KB Output is correct
83 Correct 187 ms 63420 KB Output is correct
84 Correct 169 ms 60660 KB Output is correct
85 Correct 311 ms 65212 KB Output is correct
86 Correct 189 ms 62588 KB Output is correct
87 Correct 189 ms 62528 KB Output is correct
88 Correct 188 ms 61376 KB Output is correct
89 Correct 197 ms 59196 KB Output is correct
90 Correct 305 ms 60816 KB Output is correct
91 Correct 293 ms 61620 KB Output is correct
92 Correct 303 ms 62944 KB Output is correct
93 Correct 276 ms 62296 KB Output is correct
94 Correct 242 ms 59976 KB Output is correct
95 Correct 167 ms 64564 KB Output is correct
96 Correct 128 ms 61848 KB Output is correct
97 Correct 138 ms 46056 KB Output is correct
98 Correct 251 ms 62244 KB Output is correct
99 Correct 207 ms 59016 KB Output is correct
100 Correct 54 ms 32856 KB Output is correct
101 Correct 308 ms 62588 KB Output is correct
102 Correct 212 ms 55228 KB Output is correct
103 Correct 251 ms 56252 KB Output is correct
104 Correct 332 ms 60224 KB Output is correct