Submission #601519

# Submission time Handle Problem Language Result Execution time Memory
601519 2022-07-22T06:54:35 Z 이동현(#8475) Stranded Far From Home (BOI22_island) C++17
0 / 100
321 ms 80644 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int NS = (int)4e5 + 4;

int n, m;
int a[NS];
vector<int> way[NS], way2[NS];
int pr[NS], mesum[NS], chk[NS], ans[NS];
set<pair<int, int>> st[NS];
int nodeN;

int fd(int x){
    assert(x != 0);
    return (x == pr[x] ? x : pr[x] = fd(pr[x]));
}

void unite(int x, int y){
    x = fd(x), y = fd(y);
    if(x == y) return;
    if((int)st[x].size() > (int)st[y].size()){
        swap(x, y);
    }
    pr[x] = y;
    mesum[y] += mesum[x];
    for(auto&i:st[x]){
        st[y].insert(i);
    }
}

void dfs(int x){
    for(auto&nxt:way2[x]){
        ans[nxt] += ans[x];
        dfs(nxt);
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    nodeN = n;
    vector<vector<int>> srt;
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
        srt.push_back({a[i], i});
    }
    for(int i = 0; i < m; ++i){
        int x, y; cin >> x >> y;
        way[x].push_back(y), way[y].push_back(x);
    }
    sort(srt.begin(), srt.end());
    for(int i = 0; i < n; ++i){
        int now = srt[i][1];
        mesum[now] = a[now];
        chk[now] = 1; pr[now] = now;
        for(auto&nxt:way[now]){
            if(chk[nxt]){
                int npr = fd(nxt);
                auto p = st[npr].lower_bound({a[now], now});
                if(p != st[npr].end() && p->second == now){
                    st[npr].erase(p);
                }
            }
            else{
                st[now].insert({a[nxt], nxt});
            }
        }
        for(auto&nxt:way[now]){
            if(!chk[nxt]){
                continue;
            }
            unite(now, nxt);
        }
        int u = fd(now);
        if((int)st[u].size() && mesum[u] < st[u].begin()->first){
            ++nodeN;
            pr[u] = pr[nodeN] = nodeN;
            way2[nodeN].push_back(u);
            ans[u] = 1;
        }
    }
    for(int i = 1; i <= n; ++i){
        if(fd(i) != fd(1)){
            cout << string(n, '0') << '\n';
            return 0;
        }
    }
    dfs(nodeN);
    for(int i = 1; i <= n; ++i){
        cout << (!ans[i]);
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 37944 KB Output is correct
2 Correct 18 ms 37844 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Incorrect 20 ms 38240 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 37844 KB Output is correct
2 Correct 19 ms 37844 KB Output is correct
3 Incorrect 217 ms 76468 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 37884 KB Output is correct
2 Incorrect 321 ms 80644 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 37844 KB Output is correct
2 Incorrect 314 ms 80136 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 23 ms 37944 KB Output is correct
2 Correct 18 ms 37844 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Incorrect 20 ms 38240 KB Output isn't correct
5 Halted 0 ms 0 KB -