답안 #736129

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
736129 2023-05-05T08:58:48 Z DAleksa Stranded Far From Home (BOI22_island) C++17
0 / 100
288 ms 41520 KB
#include <bits/stdc++.h>

using namespace std;

struct DSU {
    vector<int> par;
    int n;
    DSU(int _n) {
        n = _n;
        par.resize(n);
        for(int i = 0; i < n; i++) par[i] = i;
    }
    int find_par(int u) {
        if(u == par[u]) return par[u];
        return par[u] = find_par(par[u]);
    }
    bool check(int u, int v) { return find_par(u) == find_par(v); }
    void join(int u, int v) {
        u = find_par(u);
        v = find_par(v);
        par[v] = u;
    }
};

const int N = 2e5 + 10;
int n, m;
int a[N];
vector<int> g[N];
int order[N];
set<int> can[N];
bool done[N];
long long sz[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    iota(order + 1, order + n + 1, 1);
    sort(order + 1, order + n + 1, [&](int i, int j) { return a[i] < a[j]; });
    DSU dsu(n + 1);
    for(int i = 1; i <= n; i++) {
        can[i].insert(i);
        sz[i] = a[i];
    }
    for(int i = 1; i <= n; i++) {
        int u = order[i];
        for(int v : g[u]) {
            if(!done[v]) continue;
            int U = dsu.find_par(u);
            int V = dsu.find_par(v);
            if(sz[V] < sz[U]) {
                can[V].clear();
                dsu.join(u, v);
                sz[U] += sz[V];
            } else {
                if(can[U].size() < can[V].size()) swap(U, V);
                dsu.join(U, V);
                sz[U] += sz[V];
                for(auto it = can[V].begin(); it != can[V].end(); it++) can[U].insert(*it);
            }
        }
        done[u] = true;
    }
    int comp = dsu.find_par(1);
    for(int i = 1; i <= n; i++) {
        if(can[comp].find(i) == can[comp].end()) cout << 0;
        else cout << 1;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14424 KB Output is correct
3 Correct 8 ms 14372 KB Output is correct
4 Incorrect 9 ms 14548 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14424 KB Output is correct
2 Correct 8 ms 14428 KB Output is correct
3 Incorrect 183 ms 41520 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Incorrect 254 ms 38596 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14424 KB Output is correct
2 Incorrect 288 ms 38820 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14424 KB Output is correct
3 Correct 8 ms 14372 KB Output is correct
4 Incorrect 9 ms 14548 KB Output isn't correct
5 Halted 0 ms 0 KB -