QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#603676 | #6423. Fireworks | lonelywolf | TL | 0ms | 0kb | C++20 | 1.2kb | 2024-10-01 18:17:28 | 2024-10-01 18:17:28 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
for (int i = 2; i <= n; i++) {
int p;
cin >> p;
adj[p].push_back(i);
}
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
vector<int> sz(n + 1);
vector dp(n + 1, vector(n + 1, vector<ll>(2, 1e18)));
function<void(int)> dfs = [&](int x) {
sz[x] = 1;
for (auto y : adj[x]) {
dfs(y);
sz[x] += sz[y];
}
dp[x][0][0] = v[x];
dp[x][1][1] = 0;
int p = 1;
for (auto y : adj[x]) {
vector ndp(sz[x] + 1, vector<ll>(2, 1e18));
for (int i = p; i >= 0; i--) {
for (int j = 0; j <= sz[y] && i + j <= sz[x]; j++) {
ndp[i + j][0] = min(ndp[i + j][0], dp[x][i][0] + min(dp[y][j][1], dp[y][j][0] + v[y]));
ndp[i + j][1] = min(ndp[i + j][1], dp[x][i][1] + min(dp[y][j][1], dp[y][j][0]));
}
}
dp[x] = ndp;
p += sz[x];
}
};
dfs(1);
for (int i = 0; i <= n; i++) {
cout << min(dp[1][i][0], dp[1][i][1]) << " \n"[i == n];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 1 1 5000 1 1 1 1 2 10000
output:
1 0 0 0 0 0 0 0 0 94372611607200 94372611327200 94372611327232 94372611327264 94372611327296 94372611327328 94372611327360 94372611327392 94372611327424 94372611327456 94372611327488 94372611327520 94372611327552 94372611327584 94372611327616 94372611327648 94372611327680 94372611327712 943726113277...