QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#539317#4815. Flower's LandgubshigWA 2ms6432kbC++172.3kb2024-08-31 14:30:082024-08-31 14:30:09

Judging History

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

  • [2024-08-31 14:30:09]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6432kb
  • [2024-08-31 14:30:08]
  • 提交

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) {
    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].resize(Adj[v].size() - (p != 0) + 1);
    Dp[v][0].resize(1);

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

    int it = 1;
    sort(Adj[v].begin(), Adj[v].end(), [&] (int a, int b) {
        return Dp[a].back().size() < Dp[b].back().size();
    });
    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());

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

        auto prev_cdp = cumult_dp;
        Comb(prev_cdp, Dp[v][it]);
        Extend(prev_cdp, v);

        Dfs2(nxt, prev_cdp, v);

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

        Extend(Dp[nxt].back(), nxt);
        Comb(cumult_dp, Dp[nxt].back());
    }
}

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] << " ";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5512kb

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

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

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

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: 0
Accepted
time: 0ms
memory: 6140kb

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 2373 2373 1854 1940 1853 1536 2508 1945 2508 1945 2039 1827 

result:

ok 20 numbers

Test #6:

score: 0
Accepted
time: 0ms
memory: 5488kb

input:

40 5
1105 1687 737 6321 7793 7325 3443 2983 6912 6304 4211 5325 7774 7857 5121 8331 9317 1042 8291 9698 7373 440 9788 7938 7191 5563 4554 596 9733 4920 5398 3642 844 5733 4048 4417 8279 3054 4596 3153
12 17
12 36
12 15
12 13
12 2
12 30
12 18
12 33
12 4
12 39
12 25
12 20
12 10
12 9
12 23
12 29
12 3
1...

output:

35649 36231 35281 40865 42337 41869 37987 37527 41456 40848 38755 43861 42318 42401 39665 42875 43861 35586 42835 43861 41917 34984 43861 42482 41735 40107 39098 35140 43861 39464 39942 38186 35388 40277 38592 38961 42823 37598 39140 37697 

result:

ok 40 numbers

Test #7:

score: 0
Accepted
time: 0ms
memory: 5428kb

input:

100 10
11845 7520 37311 70194 67214 68176 40075 13721 13118 2555 27023 65012 36716 47598 62807 83049 95169 73454 955 72471 72461 38753 7766 53638 20670 21008 37771 97099 75063 80585 66232 33603 92301 21230 20888 96576 51530 90712 95603 93535 59988 78079 96958 42006 35041 22283 35258 7871 45967 7101 ...

output:

777955 640628 803421 836304 843042 834286 806185 779831 579059 768665 688155 851049 812544 813708 791287 836304 836304 849282 767065 848299 848289 804863 773876 829466 485824 579059 803881 614459 579059 627078 585962 735460 564264 614459 688155 836304 827358 836304 851049 792569 616369 851049 792569...

result:

ok 100 numbers

Test #8:

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

input:

200 30
332408 397037 98648 388661 351913 146463 354518 148254 131018 214842 465228 340704 397726 443065 248835 223274 239234 260094 420186 444939 5604 433696 253672 122804 20775 178018 260524 40342 259288 399226 146027 273111 141307 489658 150962 170539 339581 10624 479316 460184 467714 261732 46024...

output:

8467030 8324860 9617392 8550455 8359951 8715141 9584815 9799514 10012956 8550455 10066359 8550455 8550455 8986218 8986218 8825653 8762772 8550455 9726253 8550455 8467030 8825653 8839704 10066359 8986218 10066359 8304862 9639651 9859760 9617392 8868772 8514159 9426790 9859760 8498063 8986218 8324860 ...

result:

ok 200 numbers

Test #9:

score: -100
Wrong Answer
time: 2ms
memory: 6288kb

input:

500 50
431046 133020 268267 491421 73671 392721 149244 135041 434815 18486 70564 263715 274349 404449 348097 483453 364113 43751 27061 195734 120574 431695 191727 280919 428553 367375 451076 52558 252110 455439 202418 248384 458283 190737 59309 282145 401725 53022 493885 265157 92783 353158 80778 61...

output:

20366165 20099795 19698680 20229707 19306723 20227402 19782211 19051032 20375973 19885123 19259866 19076748 19704511 19762890 20156234 20375973 19233722 19835639 19446421 19762890 20087277 20060994 20200319 20289511 19712344 20032061 20404625 20061150 93877753827579 19548733 19821412 20256976 195211...

result:

wrong answer 29th numbers differ - expected: '20404625', found: '93877753827579'