Submission #602209

#TimeUsernameProblemLanguageResultExecution timeMemory
602209VanillaStranded Far From Home (BOI22_island)C++17
100 / 100
364 ms35044 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
const int maxn = 2e5 + 2;
int a[maxn], dsu[maxn];
int64 sum[maxn];
vector <int> ad [maxn];
vector <int> comp [maxn];
int rs [maxn];

int dad (int x) {
    return dsu[x] = (x == dsu[x] ? x: dad(dsu[x]));
}

int main() {
    int n,m;
    cin >> n >> m;
    vector <pair <int, int> > v;
    for (int i = 1; i <= n; i++){
        cin >> a[i];
        dsu[i] = i;
        sum[i] = a[i];
        comp[i].push_back(i);
        v.push_back({a[i], i});
    }
    for (int i = 1; i <= m; i++){
        int x,y;
        cin >> x >> y;
        if (a[x] < a[y]) swap(x,y);
        ad[x].push_back(y);
    }
    sort(v.begin(), v.end());
    for (auto [val, x]: v) {
        for (auto y: ad[x]) {
            if (a[y] > a[x]) continue;
            int p1 = dad(x), p2 = dad(y);
            if (val > sum[p2]) comp[p2].clear();
            if (p1 != p2) {
                if (comp[p1].size() < comp[p2].size()) swap(p1, p2);
                dsu[p2] = p1;
                sum[p1] += sum[p2];
                while (!comp[p2].empty()) {
                    comp[p1].push_back(comp[p2].back());
                    comp[p2].pop_back();
                }
            }
            ad[x].pop_back();
        }
    }
    map <int, int> dd;
    for (int i = 1; i <= n; i++){
        if (!dd.count(dad(i))) {
            dd[dad(i)] = 1;
            for (int j: comp[dad(i)]) rs[j] = 1;
        }
    }
    for (int i = 1; i <= n; i++) cout << rs[i] << "";
    return 0;
}
#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...