답안 #734180

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
734180 2023-05-02T03:35:49 Z PurpleCrayon Stranded Far From Home (BOI22_island) C++17
0 / 100
266 ms 37836 KB
#include <bits/stdc++.h>
using namespace std;
 
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 1e9+7;

// keep removing the max node, mark component as bad if it's sum is < value of the node
int n, m;
int s[N], par[N];
ll sum[N];
vector<int> st[N], good[N], adj[N];
bool ans[N];

void mark_bad(int c) {
    c = par[c];
    for (int x : good[c]) ans[x] = 0;
    good[c].clear();
}

void union_sets(int a, int b) {
    a = par[a], b = par[b];
    if (a == b) return;
    if (sz(st[a]) < sz(st[b])) swap(a, b);
    for (int x : st[b]) {
        st[a].push_back(x);
    }
    st[b].clear();
    for (int x : good[b]) {
        good[a].push_back(x);
    }
    good[b].clear();
    sum[a] += sum[b], sum[b] = 0;
}

void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }

    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b, --a, --b;
        adj[a].push_back(b), adj[b].push_back(a);
    }

    for (int i = 0; i < n; i++) {
        par[i] = i; sum[i] = s[i];
        st[i].push_back(i);
        good[i].push_back(i);
    }

    vector<int> ord(n); iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int x, int y) { return s[x] < s[y]; });
    fill(ans, ans + n, 1);
    vector<bool> hit(n);
    for (int x : ord) {
        for (int nxt : adj[x]) if (hit[nxt]) {
            int v = par[nxt];
            if (sum[v] < s[x]) {
                mark_bad(v);
            }
        }

        for (int nxt : adj[x]) if (hit[nxt]) {
            union_sets(x, nxt);
        }

        hit[x] = 1;
    }

    for (int i = 0; i < n; i++) cout << ans[i];
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14376 KB Output is correct
2 Correct 9 ms 14464 KB Output is correct
3 Correct 10 ms 14420 KB Output is correct
4 Incorrect 12 ms 14548 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Incorrect 158 ms 37836 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Incorrect 266 ms 37332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 14420 KB Output is correct
2 Incorrect 242 ms 37780 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14376 KB Output is correct
2 Correct 9 ms 14464 KB Output is correct
3 Correct 10 ms 14420 KB Output is correct
4 Incorrect 12 ms 14548 KB Output isn't correct
5 Halted 0 ms 0 KB -