QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#648092#6423. Fireworksshabi666#RE 0ms0kbC++201.8kb2024-10-17 16:59:382024-10-17 16:59:39

Judging History

你现在查看的是最新测评结果

  • [2024-10-17 16:59:39]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-10-17 16:59:38]
  • 提交

answer

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;

const int N = 2e3 + 5;

vector<int> edges[N];
int f[N][N][2], g[N];
int cnt[N];
int sz[N];
int hp[N];

void dfs0(int u) {
    g[u] = hp[u];
    sz[u] = 1;
    for (int v : edges[u]) {
        g[u] += hp[v];
    }
    for (int v : edges[u]) {
        dfs0(v);
        sz[u] += sz[v];
        g[u] += g[v];
    }
}

void dfs(int u, int n) {
    cnt[u] = 1;
    for (int i = 0; i <= n; i++) {
        f[u][i][0] = f[u][i][1] = 1e18;
    }
    f[u][0][0] = g[u];
    f[u][sz[u]][1] = 0;
    f[u][sz[u] - 1][0] = hp[u];
    for (int v : edges[u]) {
        dfs(v, n);
        for (int j = 0; j <= cnt[u] + cnt[v]; j++) {
            for (int k = 0; k <= min(cnt[v], j); k++) {
                if (j - k - 1 >= 0)
                    f[u][j][1] = min(f[u][j][1], f[u][j - k - 1][1] + min(f[v][k][0] + hp[v], f[v][k][1]));
                f[u][j][0] = min(f[u][j][0], f[u][j - k][0] + min(f[v][k][0] + hp[v], f[v][k][1])); 
            }
        }
        cnt[u] += cnt[v];
    }
}

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        edges[i].clear();
        for (int j = 0; j <= n; j++) {
            f[i][j][0] = f[i][j][1] = 0;
        }
    }
    for (int i = 2; i <= n; i++) {
        int p;
        cin >> p;
        edges[p].emplace_back(i);
    }
    for (int i = 1; i <= n; i++) {
        cin >> hp[i];
    }
    
    dfs0(1);
    dfs(1, n);
    for (int i = 0; i <= n; i++) {
        cout << min(f[1][i][0], f[1][i][1]) << " \n"[i == n];
    }
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    int T = 1;
    cin >> T;
    while (T--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

3
1 1 5000
1 1 1
1 2 10000

output:


result: