Submission #403850

# Submission time Handle Problem Language Result Execution time Memory
403850 2021-05-13T14:15:13 Z CrouchingDragon Džumbus (COCI19_dzumbus) C++17
110 / 110
59 ms 3140 KB
#include "bits/stdc++.h"

using namespace std;

#define endl '\n'
#define debug(x) cerr << #x << " == " << (x) << '\n';
#define all(x) begin(x), end(x)

using ll = long long;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

template<typename T>
void chmin(T& x, T y) { x = min(x, y); }

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

    int N, M;
    cin >> N >> M;

    vector<int> D(N);
    for (auto& x : D) cin >> x;

    vector<vector<int>> E(N);
    for (int j = 0; j < M; ++j) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        E[u].push_back(v), E[v].push_back(u);
    }

    vector<bool> vis(N, false);
    auto dfs = [&](auto&& self, int u) -> vector<array<ll, 2>> {
        vis[u] = true;
        vector<array<ll, 2>> dp = {{0LL, LINF}, {LINF, LINF}};
        for (auto v : E[u]) if (not vis[v]) {
            auto dpv = self(self, v);
            int k = (int)size(dp), l = (int)size(dpv);
            vector<array<ll, 2>> dpnxt(k + l - 1, {LINF, LINF});
            int add[4] = {D[u] + D[v], D[v], D[u], 0}, b[4] = {2, 1, 1, 0};
            for (int i = 0; i < k; ++i) {
                for (int j = 0; j < l; ++j) {
                    for (int mask = 0; mask < 4; ++mask) {
                        int s = mask & 1, t = mask >> 1 & 1;
                        chmin(dpnxt[i + j][s], dp[i][s] + dpv[j][t]);
                        if (i + j + b[mask] < k + l - 1) {
                            chmin(dpnxt[i + j + b[mask]][1], dp[i][s] + dpv[j][t] + add[mask]);
                        }
                    }
                }
            }
            swap(dp, dpnxt);
        }
        return dp;
    };

    vector<ll> dp(1, 0);

    for (int u = 0; u < N; ++u) {
        if (vis[u]) continue;
        auto comp = dfs(dfs, u);
        int k = (int)size(dp), l = (int)size(comp);
        vector<ll> dpnxt(k + l - 1, LINF), add(l);
        for (int j = 0; j < l; ++j) add[j] = min(comp[j][0], comp[j][1]);
        for (int i = 0; i < k; ++i) {
            for (int j = 0; j < l; ++j) {
                chmin(dpnxt[i + j], dp[i] + add[j]);
            }
        }
        swap(dp, dpnxt);
    }

    for (int i = (int)size(dp) - 2; i >= 0; --i) {
        dp[i] = min(dp[i], dp[i + 1]);
    }

    int Q;
    cin >> Q;
    for (int z = 0; z < Q; ++z) {
        ll S;
        cin >> S;
        int ans = (int)distance(begin(dp), upper_bound(all(dp), S)) - 1;
        cout << ans << endl;
    }

    exit(0);
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 588 KB Output is correct
2 Correct 16 ms 644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 588 KB Output is correct
2 Correct 16 ms 644 KB Output is correct
3 Correct 57 ms 1528 KB Output is correct
4 Correct 59 ms 1476 KB Output is correct
5 Correct 57 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 1088 KB Output is correct
2 Correct 41 ms 2216 KB Output is correct
3 Correct 44 ms 2624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 588 KB Output is correct
2 Correct 16 ms 644 KB Output is correct
3 Correct 57 ms 1528 KB Output is correct
4 Correct 59 ms 1476 KB Output is correct
5 Correct 57 ms 964 KB Output is correct
6 Correct 41 ms 1088 KB Output is correct
7 Correct 41 ms 2216 KB Output is correct
8 Correct 44 ms 2624 KB Output is correct
9 Correct 44 ms 2508 KB Output is correct
10 Correct 49 ms 3140 KB Output is correct
11 Correct 47 ms 2876 KB Output is correct