Submission #903884

#TimeUsernameProblemLanguageResultExecution timeMemory
903884LOLOLOStranded Far From Home (BOI22_island)C++17
100 / 100
224 ms40820 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 2e5 + 100;
vector <int> save[N];
int sz[N], p[N], ans[N];
ll sum[N], s[N];

int get(int a) {
    if (p[a] == 0)
        return a;

    return get(p[a]);
}

void unite(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b)
        return;

    if (sz[a] < sz[b]) {
        swap(a, b);
    }

    for (auto x : save[b])
        save[a].pb(x);

    save[b].clear();
    sz[a] += sz[b];
    sum[a] += sum[b];
    p[b] = a;
}

vector <int> ed[N];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;

    fill(sz, sz + 1 + n, 1);

    vector <pair <ll, ll>> lst;

    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        sum[i] = s[i];
        save[i].pb(i);
        lst.pb({s[i], i});
    }

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }

    sort(all(lst));

    for (auto e : lst) {
        int i = e.s;
        for (auto x : ed[i]) {
            if (s[x] > s[i])
                continue;

            int c = get(x);
            if (sum[c] < s[i]) {
                for (auto t : save[c]) {
                    ans[t] = 1;
                }

                save[c].clear();
            }

            unite(c, i);
        }
    }

    for (int i = 1; i <= n; i++)
        cout << !ans[i];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...