QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#541577#4815. Flower's LandgubshigWA 1ms6252kbC++172.5kb2024-08-31 20:04:392024-08-31 20:04:41

Judging History

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

  • [2024-08-31 20:04:41]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6252kb
  • [2024-08-31 20:04:39]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int MAX_N = 40001, MAX_K = 3001;

using ll = long long;

int N, K;
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) {
    if (dp.size() <= K) 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) {
    Dp[v].push_back(0);

    for (auto &nxt: Adj[v]) {
        if (nxt != p) Dfs1(nxt, v);
    }

    for (auto &nxt: Adj[v]) {
        if (nxt == p) continue;

        auto nxt_dp = Dp[nxt];
        Extend(nxt_dp, nxt);
        Comb(Dp[v], nxt_dp);
    }

}

void Dfs2(int v, vector<ll> par_dp={0}, int p=0) {
    vector<vector<ll>> cdp(Adj[v].size() - (p != 0) + 1);
    cdp[0] = par_dp;
    int it = 1;
    for (auto &nxt: Adj[v]) {
        if (nxt == p) continue;
        
        cdp[it] = cdp[it - 1];
        auto nxt_dp = Dp[nxt];
        Extend(nxt_dp, nxt);
        Comb(cdp[it], nxt_dp);
        
        it++;
    }

    reverse(Adj[v].begin(), Adj[v].end());

    it = (int) cdp.size() - 1;
    vector<ll> cumult_dp = {0};

    for (auto &nxt: Adj[v]) {
        if (nxt == p) continue;
        it--;

        auto prev_cdp = cdp[it];
        Extend(prev_cdp, v);

        Dfs2(nxt, prev_cdp, v);

        for (int i = 0; i < min((int) prev_cdp.size(), K); i++) {
            if (K - i - 1 < Dp[nxt].size()) {
                Ans[nxt] = max(Ans[nxt], prev_cdp[i] + Dp[nxt][K - i - 1]);
            }
        }
        Ans[nxt] += A[nxt];

        Extend(Dp[nxt], nxt);
        for (int i = 0; i < it; i++) {
            Comb(cdp[it], Dp[nxt]);
        }
    }
}

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);
    }

    int root = 1;
    Dfs1(root);
    Ans[root] = Dp[root][K - 1] + A[root];
    Dfs2(root);

    for (int i = 1; i <= N; i++) {
        cout << Ans[i] << " ";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5636kb

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: 5696kb

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: 5480kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5708kb

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: 6252kb

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 1827 

result:

wrong answer 9th numbers differ - expected: '2373', found: '2039'