Submission #1109515

# Submission time Handle Problem Language Result Execution time Memory
1109515 2024-11-07T01:29:28 Z Zero_OP Stranded Far From Home (BOI22_island) C++14
20 / 100
143 ms 37192 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 5;

int N, M, a[MAX];
vector<int> adj[MAX];

namespace subtask1{
    bool check(){
        return N <= 2000 && M <= 2000;
    }

    void solve(){
        vector<bool> vis(N);
        auto solve = [&](int s) -> bool{
            priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
            fill(vis.begin(), vis.end(), false);

            long long have = a[s];
            vis[s] = true;
            for(int u : adj[s]){
                if(!vis[u]){
                    vis[u] = true;
                    pq.push({a[u], u});
                }
            }

            while(!pq.empty()){
                int demand, u;
                tie(demand, u) = pq.top(); pq.pop();
                if(demand > have) return false;
                have += demand;

                for(auto v : adj[u]){
                    if(!vis[v]){
                        vis[v] = true;
                        pq.push({a[v], v});
                    }
                }
            }

            return true;
        };

        for(int i = 0; i < N; ++i){
            cout << solve(i);
        }
    }
}

namespace subtask2{
    bool check(){
        for(int i = 1; i < N; ++i){
            if(a[i - 1] < a[i]) return false;
            int lower = 0;
            for(int j : adj[i]){
                if(j < i){
                    ++lower;
                    if(lower == 2) return false;
                }
            }
        }

        return true;
    }

    int diff[MAX], tin[MAX], tout[MAX], tour[MAX], timer_dfs;
    long long sum_subtree[MAX];

    void block(int l, int r){
        ++diff[l];
        --diff[r + 1];
    }

    void pre_dfs(int u, int p){
        tour[tin[u] = timer_dfs++] = u;
        sum_subtree[u] = a[u];
        for(int v : adj[u]) if(v != p){
            pre_dfs(v, u);
            sum_subtree[u] += sum_subtree[v];
        }

        tout[u] = timer_dfs - 1;
    }

    void dfs_solve(int u, int p){
        for(int v : adj[u]) if(v != p){
            dfs_solve(v, u);

            if(a[u] > sum_subtree[v]){
                block(tin[v], tout[v]);
            }
        }
    }

    void solve(){
        pre_dfs(0, -1);
        dfs_solve(0, -1);

        for(int i = 1; i < N; ++i){
            diff[i] += diff[i - 1];
        }

        vector<bool> answer(N, true);
        for(int i = 0; i < N; ++i){
            if(diff[i] > 0){
                answer[tour[i]] = false;
            }
        }

        for(int i = 0; i < N; ++i){
            cout << answer[i];
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    cin >> N >> M;
    for(int i = 0; i < N; ++i){
        cin >> a[i];
    }

    while(M--){
        int u, v;
        cin >> u >> v;
        --u, --v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    if(subtask1::check()) return subtask1::solve(), 0;
    if(subtask2::check()) return subtask2::solve(), 0;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5456 KB Output is correct
2 Correct 2 ms 5456 KB Output is correct
3 Correct 2 ms 5456 KB Output is correct
4 Correct 125 ms 5640 KB Output is correct
5 Correct 112 ms 5624 KB Output is correct
6 Correct 143 ms 5456 KB Output is correct
7 Correct 122 ms 5456 KB Output is correct
8 Correct 87 ms 5456 KB Output is correct
9 Correct 133 ms 5456 KB Output is correct
10 Correct 49 ms 5640 KB Output is correct
11 Correct 44 ms 5628 KB Output is correct
12 Correct 37 ms 5636 KB Output is correct
13 Correct 82 ms 5456 KB Output is correct
14 Correct 63 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5632 KB Output is correct
2 Correct 2 ms 5456 KB Output is correct
3 Correct 85 ms 23244 KB Output is correct
4 Correct 96 ms 26440 KB Output is correct
5 Correct 82 ms 21016 KB Output is correct
6 Correct 90 ms 21576 KB Output is correct
7 Correct 86 ms 21576 KB Output is correct
8 Correct 90 ms 21588 KB Output is correct
9 Correct 81 ms 21244 KB Output is correct
10 Correct 69 ms 21352 KB Output is correct
11 Correct 74 ms 21184 KB Output is correct
12 Correct 81 ms 20048 KB Output is correct
13 Correct 93 ms 35656 KB Output is correct
14 Correct 94 ms 35656 KB Output is correct
15 Correct 108 ms 37192 KB Output is correct
16 Correct 84 ms 36168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5456 KB Output is correct
2 Incorrect 60 ms 12448 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5456 KB Output is correct
2 Incorrect 77 ms 12520 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5456 KB Output is correct
2 Correct 2 ms 5456 KB Output is correct
3 Correct 2 ms 5456 KB Output is correct
4 Correct 125 ms 5640 KB Output is correct
5 Correct 112 ms 5624 KB Output is correct
6 Correct 143 ms 5456 KB Output is correct
7 Correct 122 ms 5456 KB Output is correct
8 Correct 87 ms 5456 KB Output is correct
9 Correct 133 ms 5456 KB Output is correct
10 Correct 49 ms 5640 KB Output is correct
11 Correct 44 ms 5628 KB Output is correct
12 Correct 37 ms 5636 KB Output is correct
13 Correct 82 ms 5456 KB Output is correct
14 Correct 63 ms 5456 KB Output is correct
15 Correct 3 ms 5632 KB Output is correct
16 Correct 2 ms 5456 KB Output is correct
17 Correct 85 ms 23244 KB Output is correct
18 Correct 96 ms 26440 KB Output is correct
19 Correct 82 ms 21016 KB Output is correct
20 Correct 90 ms 21576 KB Output is correct
21 Correct 86 ms 21576 KB Output is correct
22 Correct 90 ms 21588 KB Output is correct
23 Correct 81 ms 21244 KB Output is correct
24 Correct 69 ms 21352 KB Output is correct
25 Correct 74 ms 21184 KB Output is correct
26 Correct 81 ms 20048 KB Output is correct
27 Correct 93 ms 35656 KB Output is correct
28 Correct 94 ms 35656 KB Output is correct
29 Correct 108 ms 37192 KB Output is correct
30 Correct 84 ms 36168 KB Output is correct
31 Correct 2 ms 5456 KB Output is correct
32 Incorrect 60 ms 12448 KB Output isn't correct
33 Halted 0 ms 0 KB -