답안 #1099218

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1099218 2024-10-10T19:57:24 Z f0rizen Džumbus (COCI19_dzumbus) C++17
50 / 110
254 ms 22228 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

template<typename T>
istream &operator>>(istream &is, vector<T> &a) {
    for (auto &i : a) {
        is >> i;
    }
    return is;
}

vector<int> a;
vector<vector<int>> g;
vector<int> sz;
vector<vector<vector<ll>>> dp;

void dfs(int v, int p = -1) {
    sz[v] = 1;
    for (auto u : g[v]) {
        if (u != p) {
            dfs(u, v);
            sz[v] += sz[u];
        }
    }
    dp[v].resize(2, vector<ll>(3, infll));
    dp[v][0][0] = 0;
    dp[v][0][1] = a[v];
    int cur = 1;
    for (auto u : g[v]) {
        if (u != p) {
            vector<vector<ll>> dpp(cur + sz[u] + 1, vector<ll>(3, infll));
            for (int i = 0; i <= cur; ++i) {
                for (int j = 0; j <= sz[u]; ++j) {
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][0]);
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][1]);
                    dpp[i + j][0] = min(dpp[i + j][0], dp[v][i][0] + dp[u][j][2]);
                    dpp[i + j][1] = min(dpp[i + j][1], dp[v][i][1] + dp[u][j][0]);
                    dpp[i + j][2] = min(dpp[i + j][2], dp[v][i][2] + dp[u][j][0]);
                    if (i + j + 1 <= cur + sz[u]) {
                        dpp[i + j + 1][2] = min(dpp[i + j + 1][2], dp[v][i][2] + dp[u][j][1]);
                    }
                    dpp[i + j][2] = min(dpp[i + j][2], dp[v][i][2] + dp[u][j][2]);
                    if (i + j + 2 <= cur + sz[u]) {
                        dpp[i + j + 2][2] = min(dpp[i + j + 2][2], dp[v][i][1] + dp[u][j][1]);
                    }
                    if (i + j + 1 <= cur + sz[u]) {
                        dpp[i + j + 1][2] = min(dpp[i + j + 1][2], dp[v][i][1] + dp[u][j][2]);
                    }
                }
            }
            dp[v] = dpp;
            cur += sz[u];
        }
    }
}

int32_t main() {
#ifdef LOCAL
    freopen("/tmp/input.txt", "r", stdin);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int n, m;
    cin >> n >> m;
    assert(m == n - 1);
    a.resize(n);
    cin >> a;
    g.resize(n);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    sz.resize(n);
    dp.resize(n);
    dfs(0);
    int q;
    cin >> q;
    while (q--) {
        int s;
        cin >> s;
        for (int i = n; i >= 0; --i) {
            if (dp[0][i][0] <= s || dp[0][i][1] <= s || dp[0][i][2] <= s) {
                cout << i << "\n";
                break;
            }
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 15700 KB Output is correct
2 Correct 37 ms 18340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 15700 KB Output is correct
2 Correct 37 ms 18340 KB Output is correct
3 Correct 90 ms 22228 KB Output is correct
4 Correct 84 ms 18060 KB Output is correct
5 Correct 254 ms 16880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 2648 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 15700 KB Output is correct
2 Correct 37 ms 18340 KB Output is correct
3 Correct 90 ms 22228 KB Output is correct
4 Correct 84 ms 18060 KB Output is correct
5 Correct 254 ms 16880 KB Output is correct
6 Correct 21 ms 2648 KB Output is correct
7 Runtime error 1 ms 604 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -