QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#287977#4815. Flower's Landzzy0922WA 1ms7024kbC++142.7kb2023-12-21 13:43:232023-12-21 13:43:23

Judging History

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

  • [2023-12-21 13:43:23]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:7024kb
  • [2023-12-21 13:43:23]
  • 提交

answer

#include <bits/stdc++.h>

inline void chkmax(int &x, const int &v) {
    x = std::max(x, v);
} 

const int N = 40005;

int n, k;
int a[N];
std::vector<int> t[N];

bool vis[N];
int rt;
int siz[N], mn, sum;

void calc_siz(int u, int f) {
    siz[u] = 1;
    int msiz = 0;
    for (const int &v : t[u]) {
        if (v == f || vis[f]) continue;
        calc_siz(v, u);
        msiz = std::max(msiz, siz[v]); 
        siz[u] += siz[v]; 
    }
    msiz = std::max(msiz, sum - siz[u]);
    if (msiz < mn) {
        rt = u;
        mn = msiz;
    }
}

void get_rt(int u) {
    sum = siz[u];
    mn = n;
    calc_siz(u, -1);
    calc_siz(rt, -1);
}

int tot, dfn[N], rnk[N];

void get_dfn(int u, int f) {
    dfn[u] = ++tot;
    rnk[tot] = u;
    for (const int &v : t[u]) {
        if (v == f || vis[v]) continue;
        get_dfn(v, u);
    }
}

int f[N][3005], g[N][3005];
int ans[N];
void dfz(int u) {
    tot = 0;
    get_dfn(u, -1);    
    // memset(f, 0, sizeof f);
    // memset(g, 0, sizeof g);
    // std::cout << "\n\n";
    // for (int i = 1; i <= tot; i++) std::cout << rnk[i] << ' ' << siz[rnk[i]] << '\n';
    // std::cout << "\n\n";
    for (int i = 0; i <= tot + 1; i++){
        memset(f[i], 0xcf, sizeof(f[i]));
        memset(g[i], 0xcf, sizeof(g[i]));
    }
    f[1][0] = 0;
    for (int i = 1; i <= tot; i++) {
        for (int j = 0; j < k; j++)
            chkmax(f[i + 1][j + 1], f[i][j] + a[rnk[i]]);
    
        for (int j = 0; j <= k; j++)
            chkmax(f[i + siz[rnk[i]]][j], f[i][j]);
    }

    g[tot][0] = 0;
    for (int i = tot - 1; i >= 1; i--) {
        for (int j = 1; j <= k; j++)
            chkmax(g[i][j], g[i + 1][j - 1] + a[rnk[i + 1]]);

        for (int j = 0; j <= k; j++) 
            chkmax(g[i][j], g[i + siz[rnk[i + 1]]][j]);
    }
    for (int i = 1; i <= tot; i++) 
        for (int j = 0; j <= k - 1; j++) 
            chkmax(ans[rnk[i]], f[i][j] + g[i][k - 1 - j] + a[rnk[i]]);
    
    // std::cout << u << '\n';
    // for (int i = 1; i <= n; i++) std::cout << ans[i] << ' ';
    // std::cout << '\n';
    // exit(0);
    vis[u] = 1;
    for (const int &v : t[u]) {
        if (vis[v]) continue;
        get_rt(v);
        dfz(v);
    }
}

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cin >> n >> k;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    for (int i = 1, x, y; i <= n - 1; i++) {
        std::cin >> x >> y;
        t[x].push_back(y);
        t[y].push_back(x);
    }
    sum = n;
    mn = n;
    calc_siz(1, -1);
    calc_siz(rt, -1);
    dfz(rt);
    for (int i = 1; i <= n; i++) std::cout << ans[i] << ' ';
    std::cout << '\n';
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 6388kb

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: 0ms
memory: 6536kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

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

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: 0ms
memory: 7024kb

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

result:

wrong answer 8th numbers differ - expected: '1707', found: '1582'