제출 #1357457

#제출 시각아이디문제언어결과실행 시간메모리
1357457maya_sStranded Far From Home (BOI22_island)C++20
10 / 100
236 ms42192 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);

void init(ll n, vector<ll> &s){
    for(ll i = 1; i <= n; i++) par[i] = i, ht[i] = 1, sum[i] = s[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);
    for(auto[d, v]: p){
        for(ll node: v){
            for(ll i : g[node]) if(s[i] <= s[node]) unite(i, node);
        }
        for(ll node: v) val[node] = sum[find_par(node)];
    }
    vector<bool> ans(n+1);
    queue<ll> q;
    for(ll node: p[ma]) q.push(node);
    while(q.size()){
        ll node = q.front(); q.pop();
        if(ans[node] == 1) continue;
        ans[node] = 1;
        for(ll i: g[node]) {
            if(ans[i] == 1) continue;
            if(val[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...