Submission #1357536

#TimeUsernameProblemLanguageResultExecution timeMemory
1357536maya_sStranded Far From Home (BOI22_island)C++20
10 / 100
255 ms54800 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
vector<ll> par(200200), ht(200200), sum(200200), val(200200), last(200200);

void init(ll n, vector<ll> &s){
    for(ll i = 1; i <= n; i++) par[i] = i, ht[i] = 1, sum[i] = s[i], last[i] = i;
}

ll find_par(ll a){
    if(a == par[a]) return a;
    return par[a] = find_par(par[a]);
}

bool unite(ll a, ll b){
    ll pa = find_par(a), pb = find_par(b);
    if(pa == pb) return 0;
    if(ht[pa] < ht[pb]) swap(pa, pb);
    par[pb] = pa;
    ht[pa] = max(ht[pa], ht[pb] + 1);
    sum[pa] += sum[pb];
    return 1;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    ll n, m; cin >> n >> m;
    vector<ll> s(n+1);
    map<ll, vector<ll>> p;
    ll ma = 0;
    for(ll i = 1; i <= n; i++) cin >> s[i], p[s[i]].push_back(i), ma = max(ma, s[i]);
    vector<vector<ll>> g(n+1);
    for(ll i = 0; i < m; i++){
        ll a, b; cin >> a >> b;
        g[a].push_back(b); g[b].push_back(a);
    }
    init(n, s);
    vector<ll> depend(n+1);
    for(auto[d, v]: p){
        for(ll node: v){
            for(ll i : g[node]) if(s[i] <= s[node]) {
                depend[last[find_par(i)]] = node;
                unite(i, node);
                last[find_par(node)] = node;
            }
        }
        for(ll node: v) val[node] = sum[find_par(node)];
    }
    set<ll, greater<ll>> degs;
    for(ll i = 1; i <= n; i++) if(s[i] != ma) degs.insert(s[i]);
    vector<bool> ans(n+1);
    for(ll node: p[ma]) ans[node] = 1;
    for(ll d: degs){
        queue<ll> q;
        for(ll node: p[d]) if(ans[depend[node]] && val[node] >= s[depend[node]]) q.push(node);
        while(q.size()){
            ll node = q.front(); q.pop();
            if(ans[node]) continue;
            ans[node] = 1;
            for(ll i: g[node]) if(!ans[i] && s[i] == s[node]) q.push(i);
        }
    }
    for(ll 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...