QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#538135 | #4815. Flower's Land | gubshig | WA | 1ms | 6120kb | C++17 | 2.1kb | 2024-08-31 07:00:59 | 2024-08-31 07:00:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX_N = 40001, MAX_K = 3001;
using ll = long long;
int N, K;
vector<vector<ll>> Dp[MAX_N];
ll A[MAX_N], Ans[MAX_N];
vector<int> Adj[MAX_N];
void Comb(vector<ll> &dp1, vector<ll> &dp2) {
dp1.resize(min(K, (int) dp1.size() + (int) dp2.size() - 1) + 1);
for (int i = (int) dp1.size() - 1; i >= 0; i--) {
for (int j = min(i, (int) dp2.size() - 1); j >= 0; j--) {
dp1[i] = max(dp1[i], dp1[i - j] + dp2[j]);
}
}
}
void Extend(vector<ll> &dp, int v) {
dp.push_back(0);
for (int i = (int) dp.size() - 2; i >= 0; i--) {
dp[i + 1] = dp[i] + A[v];
}
}
void Dfs1(int v, int p=0) {
for (auto &nxt: Adj[v]) {
if (nxt != p) Dfs1(nxt, v);
}
Dp[v].resize(Adj[v].size() - (p != 0) + 1);
Dp[v][0].resize(2);
int it = 1;
for (auto &nxt: Adj[v]) {
if (nxt == p) continue;
auto nxt_dp = Dp[nxt].back();
Extend(nxt_dp, nxt);
Dp[v][it] = Dp[v][it - 1];
Comb(Dp[v][it], nxt_dp);
it++;
}
}
void Dfs2(int v, vector<ll> cumult_dp, int p=0) { //리루팅만 짜면 됨
reverse(Adj[v].begin(), Adj[v].end());
for (auto &nxt: Adj[v]) {
if (nxt == p) continue;
Dp[v].pop_back();
auto prev_cdp = cumult_dp;
Comb(prev_cdp, Dp[v].back());
Extend(prev_cdp, v);
auto nxt_cdp = prev_cdp;
auto nxt_dp = Dp[nxt].back();
Comb(prev_cdp, nxt_dp);
Extend(prev_cdp, nxt);
Ans[nxt] = prev_cdp[K];
Dfs2(nxt, nxt_cdp, v);
Comb(cumult_dp, nxt_dp);
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(0);
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> A[i];
for (int it = 0; it < N - 1; it++) {
int u, v; cin >> u >> v;
Adj[u].push_back(v);
Adj[v].push_back(u);
}
Dfs1(1);
Ans[1] = Dp[1].back()[K - 1] + A[1];
Dfs2(1, {});
for (int i = 1; i <= N; i++) {
cout << Ans[i] << " ";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 6120kb
input:
5 3 6 10 4 3 4 3 4 4 2 2 5 5 1
output:
20 20 17 17 20
result:
ok 5 number(s): "20 20 17 17 20"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5672kb
input:
7 4 1 3 2 1 7 12 17 4 6 1 4 5 1 2 5 7 6 3 2
output:
31 13 13 31 21 31 31
result:
ok 7 numbers
Test #3:
score: 0
Accepted
time: 1ms
memory: 5696kb
input:
1 1 20
output:
20
result:
ok 1 number(s): "20"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5412kb
input:
10 3 19 7 25 18 93 97 21 51 60 80 6 7 7 1 1 9 9 10 10 2 2 5 5 3 3 8 8 4
output:
159 180 169 94 180 137 137 169 159 180
result:
ok 10 numbers
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 5488kb
input:
20 3 932 609 248 720 831 253 418 482 1000 542 436 304 217 163 872 380 704 845 497 610 17 12 1 17 15 17 13 17 2 15 16 2 18 16 8 16 4 16 19 4 6 4 20 19 10 19 9 10 5 10 7 9 3 9 14 5 11 7
output:
2508 2185 1790 1945 2373 1470 1960 1707 2039 2373 1854 1940 1853 1536 2508 1945 2508 1834 2039 2107
result:
wrong answer 9th numbers differ - expected: '2373', found: '2039'