QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#516320 | #6430. Monster Hunter | yxyxyx | WA | 2ms | 3628kb | C++20 | 3.0kb | 2024-08-12 16:03:13 | 2024-08-12 16:03:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define scanf scanf_s
#define umap unordered_map
#define lowbit(x) (x&(-x))
#define ls (p<<1)
#define rs (p<<1|1)
//#define int long long
//---------------------
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll INF = 1e18 + 10;
const int inf = 1e9 + 10;
int mod = 998244353;
//---------------------
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1)ans = ans * a % mod; a = a * a % mod; b >>= 1; }return ans; }
ll inv(ll x) { return qpow(x % mod, mod - 2); }
int d4[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
int d8[8][2] = { {0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
void solve() {
int n; cin >> n;
vector<vector<int>>g(n + 1, vector<int>());
for (int i = 2; i <= n; i++) {
int x; cin >> x;
g[x].push_back(i);
}
vector<int>a(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i];
vector<vector<vector<ll>>>dp(n + 1, vector<vector<ll>>(n + 1, vector<ll>(2, INF)));
//u的子树总共打死了i只怪物,且包不包括节点u
vector<int>siz(n + 1);
auto dfs = [&](auto slef, int u)->void {
siz[u] = 1;
dp[u][0][0] = a[u];//没打死
dp[u][1][1] = 0;//打死了u
for (int v : g[u]) {
slef(slef, v);
for (int i = siz[u]; i >= 0; i--) {
for (int j = siz[v]; j >= 0; j--) {
if (i == 0 && j == 0) {
dp[u][i + j][0] = dp[u][i][0] + dp[v][j][0] + a[v];
}
else if (i == 0) {
dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][0] + min(dp[v][j][0] + a[v], dp[v][j][1]));
dp[u][i + j][1] = min(dp[u][i + j][1], dp[u][i][1] + min(dp[v][j][0] + a[v], dp[v][j][1]));
}
else if (j == 0) {
dp[u][i + j][0] = min(dp[u][i][0], dp[u][i][1]) + dp[v][j][0] + a[v];
dp[u][i + j][1] = min(dp[u][i][0], dp[u][i][1]) + dp[v][j][0];
}
else {
dp[u][i + j][0] = min({ dp[u][i + j][0],dp[u][i][0] + min(dp[v][j][1],dp[v][j][0] + a[v]) });
dp[u][i + j][1] = min({ dp[u][i + j][1],dp[u][i][1] + min(dp[v][j][0],dp[v][j][1]) });
}
}
}
siz[u] += siz[v];
}
};
dfs(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(0), cout.tie(0);
cout << fixed << setprecision(12);
//euler();
int t = 1; cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
input:
3 5 1 2 3 4 1 2 3 4 5 9 1 2 3 4 3 4 6 6 8 4 9 4 4 5 2 4 1 12 1 2 2 4 5 3 4 3 8 10 11 9 1 3 5 10 10 7 3 7 9 4 9
output:
29 16 9 4 1 0 74 47 35 25 15 11 7 3 1 0 145 115 93 73 55 42 32 22 14 8 4 1 0
result:
ok 29 tokens
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3628kb
input:
179 20 1 1 1 4 5 5 7 7 9 9 11 12 13 14 5 16 17 16 19 3 9 3 2 7 7 2 8 5 7 5 4 7 4 2 4 9 2 7 9 19 1 1 3 4 3 6 7 6 6 10 10 12 13 13 12 16 16 18 8 8 3 6 10 1 1 1 2 2 3 3 3 10 5 5 7 10 5 2 1 10 4 2 1 2 7 14 1 1 3 4 4 6 4 8 9 10 11 8 13 4 4 6 6 10 8 9 5 7 1 4 7 9 8 6 1 2 3 3 5 2 7 5 6 1 6 11 1 2 3 3 5 6 6...
output:
209 182 159 137 117 99 81 65 56 47 39 32 25 19 15 11 8 6 4 2 0 178 151 129 108 89 74 64 54 44 36 29 23 18 13 10 7 5 2 1 0 18 4 0 16 2 0 172 137 111 93 78 63 49 39 31 23 16 10 5 1 0 52 33 21 9 3 1 0 109 72 53 39 29 22 16 10 5 2 1 0 105 69 47 35 25 17 12 7 3 0 156 133 113 97 82 68 56 44 33 26 19 14 10...
result:
wrong answer 341st words differ - expected: '66', found: '63'