QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#499923#4815. Flower's Landcz_yxxWA 1ms12368kbC++172.6kb2024-07-31 20:14:472024-07-31 20:14:49

Judging History

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

  • [2024-07-31 20:14:49]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:12368kb
  • [2024-07-31 20:14:47]
  • 提交

answer

// sis puella oier
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
ll read(){
    ll xx = 0, f = 1; char ch = getchar();
    for (;!isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (; isdigit(ch); ch = getchar())
        xx = (xx << 3) + (xx << 1) + ch - '0';
    return xx * f;
}
const int N = 40010, M = 3010, inf = 2e9;
int n, k, a[N];
vector <int> e[N];
bool vis[N];
int siz[N], mx[N], rt, tot;
int ans[N];
void getrt(int x, int fa){
    siz[x] = 1, mx[x] = 0;
    for (int v : e[x])if (v != fa && !vis[v])
        getrt(v, x), siz[x] += siz[v], mx[x] = max(mx[x], siz[v]);
    mx[x] = max(mx[x], tot - siz[x]);
    if (mx[x] < mx[rt])rt = x;
}
int f[N][M], g[N][M], dep[N], dis[N];
void init(int x, int fa){
    dep[x] = dep[fa] + 1, dis[x] = dis[fa] + a[x];
    for (int i = 1; i <= k; ++i)f[x][i] = -inf;
    for (int v : e[x])if (v != fa && !vis[v])init(v, x);
}
void dfs(int x, int fa, int o){
    if (o == 0){
        for (int i = 1; i <= k; ++i)g[x][i] = f[x][i];
        for (int i = 0, v; i < e[x].size(); ++i){
            v = e[x][i]; if (v == fa || vis[v])continue;
            for (int j = 1; j < k; ++j)if (f[x][j] != inf)
                f[v][j + 1] = max(f[v][j + 1], f[x][j] + a[v]);
            dfs(v, x, o);
        }
        for (int i = 1; i <= k; ++i)f[fa][i] = max(f[fa][i], f[x][i]);
    }
    else {
        for (int i = (int)e[x].size() - 1, v; i >= 0; --i){
            v = e[x][i]; if (v == fa || vis[v])continue;
            for (int j = 1; j < k; ++j)if (f[x][j] != inf)
                f[v][j + 1] = max(f[v][j + 1], f[x][j] + a[v]);
            dfs(v, x, o);
        }
        for (int i = 1; i <= k; ++i)f[fa][i] = max(f[fa][i], f[x][i]);
        for (int i = dep[x]; i <= k; ++i)if (g[x][i] != -inf)
            for (int j = dep[x]; j - dep[x] <= k - i; ++j)if (f[x][j] != -inf)
                ans[x] = max(ans[x], g[x][i] + f[x][j] - dis[x]);
    }
}
void divide(int x){
    // cout<<x<<endl;
    dep[x] = dis[x] = 0, init(x, x); f[x][1] = a[x]; dfs(x, x, 0);
    dep[x] = dis[x] = 0, init(x, x); f[x][1] = a[x]; dfs(x, x, 1);
    // return ;
    getrt(x, x); vis[x] = true;
    for (int v : e[x])if (!vis[v])
        tot = siz[v], rt = 0, getrt(v, v), divide(rt);
}
signed main(){
    n = read(), k = read();
    for (int i = 1; i <= n; ++i)a[i] = read();
    for (int i = 1, u, v; i < n; ++i)u = read(), v = read(),
    e[u].push_back(v), e[v].push_back(u);
    mx[0] = n, tot = n, rt = 0; getrt(1, 1); divide(rt);
    for (int i = 1; i <= n; ++i)printf("%d ", ans[i]); printf("\n");
    return 0;
}


详细

Test #1:

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

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

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

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

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

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: 1ms
memory: 8892kb

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: 1ms
memory: 7052kb

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: -100
Wrong Answer
time: 0ms
memory: 12368kb

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 294958154 768665 295230118 851049 812544 813708 295042518 836304 836304 849282 767065 848299 848289 804863 773876 829466 485824 579059 803881 295345820 579059 295042729 295145621 295230118 564264 614459 295230118 836304 827358 836304 851049 295...

result:

wrong answer 9th numbers differ - expected: '579059', found: '294958154'