Submission #722999

# Submission time Handle Problem Language Result Execution time Memory
722999 2023-04-13T07:06:38 Z kas_s Stranded Far From Home (BOI22_island) C++17
0 / 100
1000 ms 14516 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;

int N;
vector<vector<int>> adj;
vector<int> cnt;

struct DSU {
    vector<int> parent, rank;
    void init(int n) {
        parent.resize(n);
        rank.resize(n);
        iota(parent.begin(), parent.end(), 0);
    }
    int get(int x) {
        if (parent[x] == x) return x;
        return parent[x] = get(parent[x]);
    }
    void merge(int a, int b) {
        a = get(a);
        b = get(b);
        if (a != b) {
            if (rank[a] < rank[b]) swap(a, b);
            parent[b] = a;
            if (rank[a] == rank[b]) rank[a]++;
        }
    }
    bool in_union(int a, int b) {
        return get(a) == get(b);
    }
    bool all_connected() {
        int a = get(1);
        for (int i = 2; i < (int)parent.size(); i++) {
            if (get(i) != a) return false;
        }
        return true;
    }
};

bool check(int a) {
    priority_queue<tuple<int, int, int>> pq;
    int s = cnt[a];
    for (auto &v: adj[a]) {
        pq.push(make_tuple(-cnt[v], a, v));
    }
    DSU ds;
    ds.init(N + 1);
    while (!pq.empty()) {
        int w, u, v;
        tie(w, u, v) = pq.top(); pq.pop();
        w = -w;
        if (w > s || ds.in_union(a, v)) continue;
        s += w;
        for (auto &b: adj[v]) {
            if (ds.in_union(a, b)) continue;
            pq.push(make_tuple(-cnt[b], v, b));
        }
        ds.merge(u, v);
        
    }
    return ds.all_connected();
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int M;
    cin >> N >> M;
    cnt.resize(N + 1);
    for (int i = 1; i <= N; i++) cin >> cnt[i];
    adj.resize(N + 1);
    for (int i = 0, u, v; i < M; i++) {
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    string res;
    for (int i = 1; i <= N; i++) {
        res.push_back((check(i) ? '1' : '0'));
    }
    cout << res;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 4 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Execution timed out 1084 ms 14516 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1074 ms 14008 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1070 ms 14348 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Incorrect 4 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -