Submission #674186

# Submission time Handle Problem Language Result Execution time Memory
674186 2022-12-23T12:05:33 Z stanislavpolyn Stranded Far From Home (BOI22_island) C++17
35 / 100
1000 ms 64148 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 28628 KB Output is correct
2 Correct 15 ms 28524 KB Output is correct
3 Correct 16 ms 28528 KB Output is correct
4 Correct 17 ms 28748 KB Output is correct
5 Correct 15 ms 28840 KB Output is correct
6 Correct 17 ms 28704 KB Output is correct
7 Correct 17 ms 28768 KB Output is correct
8 Correct 16 ms 28680 KB Output is correct
9 Correct 18 ms 28740 KB Output is correct
10 Correct 18 ms 28756 KB Output is correct
11 Correct 17 ms 28748 KB Output is correct
12 Correct 19 ms 28788 KB Output is correct
13 Correct 17 ms 28756 KB Output is correct
14 Correct 17 ms 28788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28516 KB Output is correct
2 Correct 15 ms 28536 KB Output is correct
3 Correct 186 ms 54844 KB Output is correct
4 Correct 163 ms 54600 KB Output is correct
5 Correct 231 ms 59536 KB Output is correct
6 Correct 239 ms 60696 KB Output is correct
7 Correct 247 ms 60528 KB Output is correct
8 Correct 201 ms 56188 KB Output is correct
9 Correct 207 ms 64148 KB Output is correct
10 Correct 227 ms 56452 KB Output is correct
11 Correct 199 ms 57724 KB Output is correct
12 Correct 221 ms 57156 KB Output is correct
13 Correct 171 ms 56272 KB Output is correct
14 Correct 168 ms 55720 KB Output is correct
15 Correct 187 ms 55344 KB Output is correct
16 Correct 122 ms 55124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28456 KB Output is correct
2 Correct 285 ms 61120 KB Output is correct
3 Correct 290 ms 62400 KB Output is correct
4 Correct 192 ms 56160 KB Output is correct
5 Correct 172 ms 56524 KB Output is correct
6 Correct 301 ms 62260 KB Output is correct
7 Correct 190 ms 55368 KB Output is correct
8 Correct 195 ms 55288 KB Output is correct
9 Correct 127 ms 55112 KB Output is correct
10 Correct 190 ms 56292 KB Output is correct
11 Correct 206 ms 56816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28500 KB Output is correct
2 Correct 288 ms 56476 KB Output is correct
3 Correct 301 ms 58620 KB Output is correct
4 Correct 318 ms 59992 KB Output is correct
5 Correct 264 ms 59208 KB Output is correct
6 Correct 221 ms 56676 KB Output is correct
7 Correct 175 ms 57540 KB Output is correct
8 Execution timed out 1036 ms 52068 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 28628 KB Output is correct
2 Correct 15 ms 28524 KB Output is correct
3 Correct 16 ms 28528 KB Output is correct
4 Correct 17 ms 28748 KB Output is correct
5 Correct 15 ms 28840 KB Output is correct
6 Correct 17 ms 28704 KB Output is correct
7 Correct 17 ms 28768 KB Output is correct
8 Correct 16 ms 28680 KB Output is correct
9 Correct 18 ms 28740 KB Output is correct
10 Correct 18 ms 28756 KB Output is correct
11 Correct 17 ms 28748 KB Output is correct
12 Correct 19 ms 28788 KB Output is correct
13 Correct 17 ms 28756 KB Output is correct
14 Correct 17 ms 28788 KB Output is correct
15 Correct 15 ms 28516 KB Output is correct
16 Correct 15 ms 28536 KB Output is correct
17 Correct 186 ms 54844 KB Output is correct
18 Correct 163 ms 54600 KB Output is correct
19 Correct 231 ms 59536 KB Output is correct
20 Correct 239 ms 60696 KB Output is correct
21 Correct 247 ms 60528 KB Output is correct
22 Correct 201 ms 56188 KB Output is correct
23 Correct 207 ms 64148 KB Output is correct
24 Correct 227 ms 56452 KB Output is correct
25 Correct 199 ms 57724 KB Output is correct
26 Correct 221 ms 57156 KB Output is correct
27 Correct 171 ms 56272 KB Output is correct
28 Correct 168 ms 55720 KB Output is correct
29 Correct 187 ms 55344 KB Output is correct
30 Correct 122 ms 55124 KB Output is correct
31 Correct 15 ms 28456 KB Output is correct
32 Correct 285 ms 61120 KB Output is correct
33 Correct 290 ms 62400 KB Output is correct
34 Correct 192 ms 56160 KB Output is correct
35 Correct 172 ms 56524 KB Output is correct
36 Correct 301 ms 62260 KB Output is correct
37 Correct 190 ms 55368 KB Output is correct
38 Correct 195 ms 55288 KB Output is correct
39 Correct 127 ms 55112 KB Output is correct
40 Correct 190 ms 56292 KB Output is correct
41 Correct 206 ms 56816 KB Output is correct
42 Correct 15 ms 28500 KB Output is correct
43 Correct 288 ms 56476 KB Output is correct
44 Correct 301 ms 58620 KB Output is correct
45 Correct 318 ms 59992 KB Output is correct
46 Correct 264 ms 59208 KB Output is correct
47 Correct 221 ms 56676 KB Output is correct
48 Correct 175 ms 57540 KB Output is correct
49 Execution timed out 1036 ms 52068 KB Time limit exceeded
50 Halted 0 ms 0 KB -