답안 #93393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
93393 2019-01-08T06:26:57 Z Kastanda Chase (CEOI17_chase) C++11
100 / 100
774 ms 337272 KB
// Borrowed
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
const int N = 1e5 + 5, K = 103;
int n, m, k, A[N], P[N];
long long S[N], dp[N][K][2], dp2[N][K][2], T[K][2][2];
vector < int > Adj[N];
void DFS(int v, int p)
{
    P[v] = p;
    for (int &u : Adj[v])
        if (u != p)
            S[v] += A[u];
    for (int &u : Adj[v])
        if (u != p)
        {
            DFS(u, v);
            for (int i = 0; i <= k; i++)
                dp[v][i][0] = max(dp[v][i][0], max(dp[u][i][1], dp[u][i][0]));
            for (int i = 1; i <= k; i++)
                dp[v][i][1] = max(dp[v][i][1], S[v] + max(dp[u][i-1][1], dp[u][i-1][0]));
        }
}
void DFS2(int v, int p)
{
    memset(T, 0, sizeof(T));
    for (int i = 1; i <= k; i++)
        T[i][0][0] = dp2[v][i][0], T[i][1][0] = dp2[v][i][1];
    for (int &u : Adj[v])
        if (u != p)
        {
            for (int i = 1; i <= k; i++)
            {
                long long val = max(dp[u][i][1] - A[v], dp[u][i][0]);
                if (T[i][0][1] < val)
                    T[i][0][1] = val;
                if (T[i][0][0] < T[i][0][1])
                    swap(T[i][0][0], T[i][0][1]);
            }
            for (int i = 1; i <= k; i++)
            {
                long long val = S[v] + A[P[v]] + max(dp[u][i-1][1] - A[v], dp[u][i-1][0]);
                if (T[i][1][1] < val)
                    T[i][1][1] = val;
                if (T[i][1][0] < T[i][1][1])
                    swap(T[i][1][1], T[i][1][0]);
            }
        }
    for (int &u : Adj[v])
        if (u != p)
        {
            for (int i = 1; i <= k; i++)
            {
                /// Chosen
                long long val = T[i][1][0];
                if (val == S[v] + A[P[v]] + max(dp[u][i-1][1] - A[v], dp[u][i-1][0]))
                    val = T[i][1][1];
                dp2[u][i][0] = val - A[u];
                /// Not
                val = T[i][0][0];
                if (val == max(dp[u][i][1] - A[v], dp[u][i][0]))
                    val = T[i][0][1];
                dp2[u][i][0] = max(dp2[u][i][0], val);
            }
            for (int i = 2; i <= k; i++)
            {
                /// Chosen
                long long val = T[i-1][1][0];
                if (val == S[v] + A[P[v]] + max(dp[u][i-2][1] - A[v], dp[u][i-2][0]))
                    val = T[i-1][1][1];
                dp2[u][i][1] = S[u] + A[v] + val - A[u];
                /// Not
                val = T[i-1][0][0];
                if (val == max(dp[u][i-1][1] - A[v], dp[u][i-1][0]))
                    val = T[i-1][0][1];
                dp2[u][i][1] = max(dp2[u][i][1], S[u] + A[v] + val);
            }
            dp2[u][1][1] = S[u] + A[v];
        }
    for (int &u : Adj[v])
        if (u != p)
            DFS2(u, v);
}
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &A[i]);
    for (int i = 1, v, u; i < n; i++)
        scanf("%d%d", &v, &u), Adj[v].pb(u), Adj[u].pb(v);
    DFS(1, 0);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= k; j++)
            dp[i][j][1] += A[P[i]];
    DFS2(1, 0);
    long long Mx = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= k; j++)
            for (int w = 0; w <= 1; w ++)
            {
                Mx = max(Mx, max(dp[i][j][w], dp2[i][j][w]));
                //printf("%d , %d : %lld\n", i, j, dp[i][j][0]);
            }
    printf("%lld", Mx);
    return (0);
}

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:89: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:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &A[i]);
         ~~~~~^~~~~~~~~~~~~
chase.cpp:93:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &v, &u), Adj[v].pb(u), Adj[u].pb(v);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 4 ms 2744 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 4 ms 2744 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 9 ms 6008 KB Output is correct
8 Correct 7 ms 6008 KB Output is correct
9 Correct 7 ms 6008 KB Output is correct
10 Correct 9 ms 6136 KB Output is correct
11 Correct 7 ms 6008 KB Output is correct
12 Correct 7 ms 6008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 641 ms 337244 KB Output is correct
2 Correct 645 ms 337172 KB Output is correct
3 Correct 546 ms 331688 KB Output is correct
4 Correct 422 ms 332152 KB Output is correct
5 Correct 744 ms 332268 KB Output is correct
6 Correct 774 ms 332152 KB Output is correct
7 Correct 733 ms 332216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2812 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 4 ms 2744 KB Output is correct
4 Correct 4 ms 2680 KB Output is correct
5 Correct 4 ms 2680 KB Output is correct
6 Correct 4 ms 2680 KB Output is correct
7 Correct 9 ms 6008 KB Output is correct
8 Correct 7 ms 6008 KB Output is correct
9 Correct 7 ms 6008 KB Output is correct
10 Correct 9 ms 6136 KB Output is correct
11 Correct 7 ms 6008 KB Output is correct
12 Correct 7 ms 6008 KB Output is correct
13 Correct 641 ms 337244 KB Output is correct
14 Correct 645 ms 337172 KB Output is correct
15 Correct 546 ms 331688 KB Output is correct
16 Correct 422 ms 332152 KB Output is correct
17 Correct 744 ms 332268 KB Output is correct
18 Correct 774 ms 332152 KB Output is correct
19 Correct 733 ms 332216 KB Output is correct
20 Correct 632 ms 332156 KB Output is correct
21 Correct 526 ms 284920 KB Output is correct
22 Correct 656 ms 332148 KB Output is correct
23 Correct 365 ms 332152 KB Output is correct
24 Correct 674 ms 332124 KB Output is correct
25 Correct 517 ms 331276 KB Output is correct
26 Correct 672 ms 337252 KB Output is correct
27 Correct 643 ms 337272 KB Output is correct