Submission #723045

# Submission time Handle Problem Language Result Execution time Memory
723045 2023-04-13T07:54:09 Z drdilyor Stranded Far From Home (BOI22_island) C++17
10 / 100
1000 ms 116388 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

struct dsu {
    vector<int> par;
    vector<vector<int>> el;
    vector<set<int>> ed;
    dsu(int n) : par(n), el(n), ed(n) {
        for (int i = 0; i < n; i++) par[i] = i, el[i] = {i};
    }
    int get(int i) {
        return par[i] == i ? par[i] : par[i] = get(par[i]);
    }
    void edge(int i, int j) {
        ed[get(i)].insert(j);
    }
    void merge(int a, int b) {
        a = get(a);
        b = get(b);
        if (el[a].size() < el[b].size())
            swap(a, b);
        if (a != b) {
            par[b] =a;
            for (int x : el[b])
                el[a].push_back(x);
            if (ed[a].size() < ed[b].size())
                swap(ed[a], ed[b]);
            for (int x : ed[b])
                ed[a].insert(x);
        }
    }
};

int main() {
    int n, m;
    cin >> n >> m;
    vector s(n, 0);
    for (int& i : s) cin >> i;

    vector<int> ix(n);
    for (int i{}; i < n ;i++) ix[i] = i;
    stable_sort(ix.begin(), ix.end(), [&](int a, int b) {
        return s[a] > s[b];
    });
    vector<int> jx(n);
    for (int i = 0; i < n; i ++)
        jx[ix[i]] = i;
    stable_sort(s.begin(), s.end(), greater{});

    vector<vector<int>> adj(n);
    for (int i = 0; i < m ;i++) {
        int u, v;
        cin >> u >> v;
        u--;v--;
        adj[jx[u]].push_back(jx[v]);
        adj[jx[v]].push_back(jx[u]);
    }

    for (int i = 0; i < n; i++) {
        sort(adj[i].begin(), adj[i].end());
    }

    //for (int i : ix) cout << i << ' ';cout << endl;
    //for (int i : jx) cout << i << ' ';cout << endl;
    //for (int i : s) cout << i << ' ';cout << endl;
    string res(n, '?');
    vector<int> back(n, -1);

    dsu cc(n);

    for (int i =n -1; i >= 0;i --) {
        for (int j : adj[i]) {
            cc.edge(i, j);
            if (j > i)
            cc.merge(i, j);
        }

        ll sum = 0;
        for (int j : cc.el[cc.get(i)]) {
            sum += s[j];
        }

        int c = cc.get(i);
        auto it = cc.ed[c].lower_bound(i);
        if (it != cc.ed[c].begin()) {
            --it;
            back[i] = max(back[i], *it);
        }

        if (back[i] != -1 && s[back[i]] > sum)
            back[i] = -1;
        if (back[i] == -1) {
            if (i == 0)
                res[i] = '1';
            else res[i] = '0';
        }
    }

    string ans(n, '?');
    for (int i = 1; i < n;i++) {
        if (back[i] != -1) res[i] = res[back[i]];
    }
    for (int i = 0; i < n; i++) {
        ans[ix[i]] = res[i];
    }
    cout << ans << '\n';
}
# 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 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 852 KB Output is correct
5 Correct 5 ms 980 KB Output is correct
6 Correct 4 ms 852 KB Output is correct
7 Correct 6 ms 852 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 5 ms 1108 KB Output is correct
12 Correct 4 ms 1236 KB Output is correct
13 Correct 4 ms 852 KB Output is correct
14 Correct 6 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 1076 ms 48044 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 Correct 803 ms 116388 KB Output is correct
3 Correct 789 ms 116292 KB Output is correct
4 Execution timed out 1082 ms 42952 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Execution timed out 1079 ms 79392 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 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 4 ms 852 KB Output is correct
5 Correct 5 ms 980 KB Output is correct
6 Correct 4 ms 852 KB Output is correct
7 Correct 6 ms 852 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 5 ms 852 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 5 ms 1108 KB Output is correct
12 Correct 4 ms 1236 KB Output is correct
13 Correct 4 ms 852 KB Output is correct
14 Correct 6 ms 980 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Execution timed out 1076 ms 48044 KB Time limit exceeded
18 Halted 0 ms 0 KB -