답안 #1109540

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109540 2024-11-07T02:37:08 Z Zero_OP Stranded Far From Home (BOI22_island) C++14
100 / 100
228 ms 38584 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];
        }
    }
}

namespace subtask3{
    bool check(){
        for(int i = 0; i < N; ++i){
            for(int j : adj[i]){
                if(abs(i - j) != 1) return false;
            }
        }
        return true;
    }

    long long pref[MAX];
    pair<int, int> rmq[20][MAX];
    int diff[MAX];

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

    pair<int, int> query(int l, int r){
        int k = 31 - __builtin_clz(r - l + 1);
        return max(rmq[k][l], rmq[k][r - (1 << k) + 1]);
    }

    long long sum(int l, int r){
        return pref[r] - (l > 0 ? pref[l - 1] : 0);
    }

    void solve(int l, int r){
        if(l >= r) return;

        int mx, mid; tie(mx, mid) = query(l, r);

        if(l < mid){
            if(sum(l, mid - 1) >= mx) solve(l, mid - 1);
            else block(l, mid - 1);
        }

        if(mid < r){
            if(sum(mid + 1, r) >= mx) solve(mid + 1, r);
            else block(mid + 1, r);
        }
    }

    void solve(){
        copy(a, a + N, pref);
        for(int i = 1; i < N; ++i){
            pref[i] += pref[i - 1];
        }

        for(int i = 0; i < N; ++i){
            rmq[0][i] = {a[i], i};
        }

        for(int i = 1; (1 << i) <= N; ++i){
            for(int j = 0; j + (1 << i) - 1 < N; ++j){
                rmq[i][j] = max(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
            }
        }

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

namespace subtask5{
    bool check(){
        return true;
    }

    int lab[MAX];
    long long sum[MAX];
    vector<int> candidates[MAX];

    int root(int u){
        return lab[u] < 0 ? u : (lab[u] = root(lab[u]));
    }

    void unite(int u, int v){
        u = root(u);
        v = root(v);
        if(u == v) return;
        if((int)candidates[u].size() < candidates[v].size()) swap(u, v);
        lab[u] += lab[v];
        lab[v] = u;
        sum[u] += sum[v];

        if(candidates[u].empty()) swap(candidates[u], candidates[v]);
        else{
            candidates[u].insert(candidates[u].end(), candidates[v].begin(), candidates[v].end());
        }
    }

    void solve(){
        vector<pair<int, int>> v;
        for(int i = 0; i < N; ++i){
            v.emplace_back(a[i], i);
            lab[i] = -1;
            candidates[i].push_back(i);
            sum[i] = a[i];
        }

        sort(v.begin(), v.end());
        vector<bool> in(N, false);

        for(auto [value, u] : v){
            vector<int> combinable;
            for(auto v : adj[u]){
                if(in[v]){
                    combinable.push_back(root(v));
                }
            }

            sort(combinable.begin(), combinable.end());
            combinable.erase(unique(combinable.begin(), combinable.end()), combinable.end());

            for(auto v : combinable){
                if(sum[v] < a[u]){
                    candidates[v].clear();
                }

                if((int)candidates[u].size() < (int)candidates[v].size()){
                    swap(candidates[u], candidates[v]);
                }

                candidates[u].insert(candidates[u].end(), candidates[v].begin(), candidates[v].end());
                candidates[v].clear();
            }

            for(auto v : combinable){
                unite(v, u);
            }

            in[u] = true;
        }

        vector<bool> answer(N, false);
        int r = root(0);
        for(int cand : candidates[r]){
            answer[cand] = true;
        }

        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;
    //if(subtask3::check()) return subtask3::solve(), 0;
    if(subtask5::check()) return subtask5::solve(), 0;

    return 0;
}

Compilation message

island.cpp: In function 'void subtask5::unite(int, int)':
island.cpp:204:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  204 |         if((int)candidates[u].size() < candidates[v].size()) swap(u, v);
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
island.cpp: In function 'void subtask5::solve()':
island.cpp:227:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  227 |         for(auto [value, u] : v){
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11344 KB Output is correct
2 Correct 3 ms 11452 KB Output is correct
3 Correct 3 ms 11344 KB Output is correct
4 Correct 4 ms 11600 KB Output is correct
5 Correct 3 ms 11600 KB Output is correct
6 Correct 4 ms 11600 KB Output is correct
7 Correct 3 ms 11600 KB Output is correct
8 Correct 3 ms 11600 KB Output is correct
9 Correct 4 ms 11600 KB Output is correct
10 Correct 4 ms 11600 KB Output is correct
11 Correct 3 ms 11600 KB Output is correct
12 Correct 4 ms 11600 KB Output is correct
13 Correct 4 ms 11600 KB Output is correct
14 Correct 4 ms 11600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11344 KB Output is correct
2 Correct 3 ms 11344 KB Output is correct
3 Correct 128 ms 30392 KB Output is correct
4 Correct 111 ms 32028 KB Output is correct
5 Correct 160 ms 32184 KB Output is correct
6 Correct 118 ms 32696 KB Output is correct
7 Correct 127 ms 32696 KB Output is correct
8 Correct 132 ms 30904 KB Output is correct
9 Correct 108 ms 34224 KB Output is correct
10 Correct 100 ms 31444 KB Output is correct
11 Correct 115 ms 31772 KB Output is correct
12 Correct 138 ms 30384 KB Output is correct
13 Correct 99 ms 33916 KB Output is correct
14 Correct 105 ms 33720 KB Output is correct
15 Correct 125 ms 30764 KB Output is correct
16 Correct 81 ms 30624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11344 KB Output is correct
2 Correct 150 ms 33212 KB Output is correct
3 Correct 161 ms 32952 KB Output is correct
4 Correct 140 ms 33976 KB Output is correct
5 Correct 101 ms 32696 KB Output is correct
6 Correct 158 ms 33208 KB Output is correct
7 Correct 118 ms 30820 KB Output is correct
8 Correct 127 ms 30904 KB Output is correct
9 Correct 103 ms 30720 KB Output is correct
10 Correct 102 ms 31416 KB Output is correct
11 Correct 119 ms 31040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11344 KB Output is correct
2 Correct 149 ms 30916 KB Output is correct
3 Correct 165 ms 35768 KB Output is correct
4 Correct 149 ms 37292 KB Output is correct
5 Correct 147 ms 37984 KB Output is correct
6 Correct 124 ms 36024 KB Output is correct
7 Correct 112 ms 36536 KB Output is correct
8 Correct 85 ms 35124 KB Output is correct
9 Correct 88 ms 25276 KB Output is correct
10 Correct 135 ms 38056 KB Output is correct
11 Correct 125 ms 34232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11344 KB Output is correct
2 Correct 3 ms 11452 KB Output is correct
3 Correct 3 ms 11344 KB Output is correct
4 Correct 4 ms 11600 KB Output is correct
5 Correct 3 ms 11600 KB Output is correct
6 Correct 4 ms 11600 KB Output is correct
7 Correct 3 ms 11600 KB Output is correct
8 Correct 3 ms 11600 KB Output is correct
9 Correct 4 ms 11600 KB Output is correct
10 Correct 4 ms 11600 KB Output is correct
11 Correct 3 ms 11600 KB Output is correct
12 Correct 4 ms 11600 KB Output is correct
13 Correct 4 ms 11600 KB Output is correct
14 Correct 4 ms 11600 KB Output is correct
15 Correct 3 ms 11344 KB Output is correct
16 Correct 3 ms 11344 KB Output is correct
17 Correct 128 ms 30392 KB Output is correct
18 Correct 111 ms 32028 KB Output is correct
19 Correct 160 ms 32184 KB Output is correct
20 Correct 118 ms 32696 KB Output is correct
21 Correct 127 ms 32696 KB Output is correct
22 Correct 132 ms 30904 KB Output is correct
23 Correct 108 ms 34224 KB Output is correct
24 Correct 100 ms 31444 KB Output is correct
25 Correct 115 ms 31772 KB Output is correct
26 Correct 138 ms 30384 KB Output is correct
27 Correct 99 ms 33916 KB Output is correct
28 Correct 105 ms 33720 KB Output is correct
29 Correct 125 ms 30764 KB Output is correct
30 Correct 81 ms 30624 KB Output is correct
31 Correct 3 ms 11344 KB Output is correct
32 Correct 150 ms 33212 KB Output is correct
33 Correct 161 ms 32952 KB Output is correct
34 Correct 140 ms 33976 KB Output is correct
35 Correct 101 ms 32696 KB Output is correct
36 Correct 158 ms 33208 KB Output is correct
37 Correct 118 ms 30820 KB Output is correct
38 Correct 127 ms 30904 KB Output is correct
39 Correct 103 ms 30720 KB Output is correct
40 Correct 102 ms 31416 KB Output is correct
41 Correct 119 ms 31040 KB Output is correct
42 Correct 3 ms 11344 KB Output is correct
43 Correct 149 ms 30916 KB Output is correct
44 Correct 165 ms 35768 KB Output is correct
45 Correct 149 ms 37292 KB Output is correct
46 Correct 147 ms 37984 KB Output is correct
47 Correct 124 ms 36024 KB Output is correct
48 Correct 112 ms 36536 KB Output is correct
49 Correct 85 ms 35124 KB Output is correct
50 Correct 88 ms 25276 KB Output is correct
51 Correct 135 ms 38056 KB Output is correct
52 Correct 125 ms 34232 KB Output is correct
53 Correct 3 ms 11344 KB Output is correct
54 Correct 4 ms 11344 KB Output is correct
55 Correct 4 ms 11344 KB Output is correct
56 Correct 5 ms 11964 KB Output is correct
57 Correct 4 ms 11600 KB Output is correct
58 Correct 4 ms 11600 KB Output is correct
59 Correct 4 ms 11600 KB Output is correct
60 Correct 5 ms 11600 KB Output is correct
61 Correct 4 ms 11600 KB Output is correct
62 Correct 4 ms 11600 KB Output is correct
63 Correct 4 ms 11600 KB Output is correct
64 Correct 4 ms 11776 KB Output is correct
65 Correct 4 ms 11600 KB Output is correct
66 Correct 4 ms 11768 KB Output is correct
67 Correct 151 ms 35000 KB Output is correct
68 Correct 114 ms 35344 KB Output is correct
69 Correct 118 ms 36264 KB Output is correct
70 Correct 143 ms 37048 KB Output is correct
71 Correct 172 ms 37048 KB Output is correct
72 Correct 143 ms 35380 KB Output is correct
73 Correct 123 ms 38560 KB Output is correct
74 Correct 134 ms 34936 KB Output is correct
75 Correct 118 ms 35000 KB Output is correct
76 Correct 170 ms 33396 KB Output is correct
77 Correct 120 ms 36996 KB Output is correct
78 Correct 118 ms 36832 KB Output is correct
79 Correct 135 ms 35256 KB Output is correct
80 Correct 93 ms 34532 KB Output is correct
81 Correct 171 ms 37560 KB Output is correct
82 Correct 182 ms 37304 KB Output is correct
83 Correct 166 ms 38584 KB Output is correct
84 Correct 162 ms 35772 KB Output is correct
85 Correct 228 ms 37564 KB Output is correct
86 Correct 179 ms 35472 KB Output is correct
87 Correct 180 ms 35452 KB Output is correct
88 Correct 148 ms 35256 KB Output is correct
89 Correct 163 ms 33972 KB Output is correct
90 Correct 172 ms 35256 KB Output is correct
91 Correct 167 ms 35696 KB Output is correct
92 Correct 160 ms 37300 KB Output is correct
93 Correct 184 ms 37964 KB Output is correct
94 Correct 132 ms 35796 KB Output is correct
95 Correct 116 ms 36536 KB Output is correct
96 Correct 86 ms 35132 KB Output is correct
97 Correct 90 ms 25332 KB Output is correct
98 Correct 154 ms 38204 KB Output is correct
99 Correct 145 ms 34136 KB Output is correct
100 Correct 36 ms 15692 KB Output is correct
101 Correct 166 ms 36024 KB Output is correct
102 Correct 163 ms 32200 KB Output is correct
103 Correct 181 ms 32056 KB Output is correct
104 Correct 203 ms 35000 KB Output is correct