답안 #44714

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
44714 2018-04-05T12:06:59 Z bogdan10bos Chase (CEOI17_chase) C++14
100 / 100
514 ms 170492 KB
#include <bits/stdc++.h>

using namespace std;

//#define FILE_IO

typedef long long LL;

LL ans;
int N, K;
int v[100005];
LL vec[100005];
LL dpup[100005][105], dpdown[100005][105];
LL bstdown[105][2];
vector<int> edg[100005];

void DFS(int nod, int fth)
{
    for(auto nxt: edg[nod])
    {
        if(nxt == fth)  continue;
        DFS(nxt, nod);
    }

    for(int i = 0; i <= K; i++)
        bstdown[i][0] = bstdown[i][1] = 0LL;

    for(auto nxt: edg[nod])
    {
        if(nxt == fth)  continue;

        for(int i = 0; i <= K; i++)
        {
            if(i > 0)
            {
                dpup[nod][i] = max({ dpup[nod][i], dpup[nxt][i], dpup[nxt][i - 1] + vec[nod] - v[nxt] });
                dpdown[nod][i] = max({ dpdown[nod][i], dpdown[nxt][i], dpdown[nxt][i - 1] + vec[nod] - v[fth] });
                dpup[nod][i] = max(dpup[nod][i], dpup[nod][i - 1]), dpdown[nod][i] = max(dpdown[nod][i], dpdown[nod][i - 1]);
                ans = max(ans, dpdown[nxt][i - 1] + vec[nod]);
            }

            ans = max(ans, dpup[nod][i]);
            ans = max(ans, dpdown[nod][i]);

            if(dpdown[nxt][i] > bstdown[i][0])
            {
                bstdown[i][1] = bstdown[i][0];
                bstdown[i][0] = dpdown[nxt][i];
            }
            else if(dpdown[nxt][i] > bstdown[i][1])
                bstdown[i][1] = dpdown[nxt][i];
        }
    }

    for(auto nxt: edg[nod])
    {
        if(nxt == fth)  continue;

        for(int i = 0; i <= K; i++)
        {
            LL nxtbst = bstdown[i][0];
            if(bstdown[i][0] == dpdown[nxt][i]) nxtbst = bstdown[i][1];

            ans = max(ans, dpup[nxt][K - i] + nxtbst);
            if(K - i - 1 >= 0)  ans = max(ans, nxtbst + dpup[nxt][K - i - 1] + vec[nod] - v[nxt]);
        }
    }

    dpup[nod][1] = max(dpup[nod][1], vec[nod]);
}

int main()
{
    #ifdef FILE_IO
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    #endif

    scanf("%d%d", &N, &K);
    for(int i = 1; i <= N; i++) scanf("%d", &v[i]);
    for(int i = 1; i < N; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        edg[x].push_back(y);
        edg[y].push_back(x);
        vec[x] += v[y];
        vec[y] += v[x];
    }

    ans = 0;
    DFS(1, 0);

    printf("%lld\n", ans);

    return 0;
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:80:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 1; i <= N; i++) scanf("%d", &v[i]);
                                 ~~~~~^~~~~~~~~~~~~
chase.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2824 KB Output is correct
3 Correct 5 ms 2824 KB Output is correct
4 Correct 5 ms 2824 KB Output is correct
5 Correct 5 ms 2824 KB Output is correct
6 Correct 4 ms 2848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2824 KB Output is correct
3 Correct 5 ms 2824 KB Output is correct
4 Correct 5 ms 2824 KB Output is correct
5 Correct 5 ms 2824 KB Output is correct
6 Correct 4 ms 2848 KB Output is correct
7 Correct 9 ms 4152 KB Output is correct
8 Correct 7 ms 4280 KB Output is correct
9 Correct 6 ms 4280 KB Output is correct
10 Correct 8 ms 4588 KB Output is correct
11 Correct 7 ms 4588 KB Output is correct
12 Correct 8 ms 4588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 391 ms 136152 KB Output is correct
2 Correct 402 ms 136196 KB Output is correct
3 Correct 298 ms 136196 KB Output is correct
4 Correct 324 ms 157880 KB Output is correct
5 Correct 459 ms 159904 KB Output is correct
6 Correct 514 ms 160028 KB Output is correct
7 Correct 459 ms 160028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2680 KB Output is correct
2 Correct 4 ms 2824 KB Output is correct
3 Correct 5 ms 2824 KB Output is correct
4 Correct 5 ms 2824 KB Output is correct
5 Correct 5 ms 2824 KB Output is correct
6 Correct 4 ms 2848 KB Output is correct
7 Correct 9 ms 4152 KB Output is correct
8 Correct 7 ms 4280 KB Output is correct
9 Correct 6 ms 4280 KB Output is correct
10 Correct 8 ms 4588 KB Output is correct
11 Correct 7 ms 4588 KB Output is correct
12 Correct 8 ms 4588 KB Output is correct
13 Correct 391 ms 136152 KB Output is correct
14 Correct 402 ms 136196 KB Output is correct
15 Correct 298 ms 136196 KB Output is correct
16 Correct 324 ms 157880 KB Output is correct
17 Correct 459 ms 159904 KB Output is correct
18 Correct 514 ms 160028 KB Output is correct
19 Correct 459 ms 160028 KB Output is correct
20 Correct 432 ms 161996 KB Output is correct
21 Correct 204 ms 161996 KB Output is correct
22 Correct 440 ms 165948 KB Output is correct
23 Correct 269 ms 166088 KB Output is correct
24 Correct 423 ms 170492 KB Output is correct
25 Correct 273 ms 170492 KB Output is correct
26 Correct 372 ms 170492 KB Output is correct
27 Correct 405 ms 170492 KB Output is correct