Submission #67978356


Source Code Expand

#include <bits/stdc++.h>
//#define int long long 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define fr first
#define se second
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int N = 2e5 + 10;

int n, k;
ll a[N];
vector<int> G[N];

void chmax(ll& x, ll y){
    x = max(x, y);
}

void solve()
{
    cin >> n >> k;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    for(int i = 1; i <= n - 1; i ++){
        int u, v;
        cin >> u >> v;
        G[u].pb(v); G[v].pb(u);
    }

    vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(7, vector<ll>(2, -INF)));
    // dp[u][j][0]: 考虑以u为根的子树,不算点u,选取恰好j条互不重叠的路径,权值总和的最大值
    // dp[u][j][1]: 考虑以u为根的子树,算点u,选取恰好j条互不重叠的路径,权值总和的最大值
    vector<vector<ll>> f(n + 1, vector<ll>(7, -INF));
    // f[u][j]:考虑以u为根的子树,选取恰好j条互不重叠的路径,其中必须存在一条路径以u为端点,权值总和的最大值

    function<void(int,int)> DFS = [&](int u, int father){
        // init
        dp[u][0][0] = 0;
        dp[u][0][1] = 0;
        dp[u][1][1] = a[u];
        f[u][1] = a[u];
        for(auto v : G[u]){
            if(v == father) continue;
            DFS(v, u);
            // !注意,在进行树形dp时,若想使用合并子树之前的结果转移,则最好(bixu)先将转移之前的结果保存下来,以避免在转移过程中被覆盖
            auto pre_dp_u = dp[u]; 
            auto pre_f_u = f[u];
            for(int j = 1; j <= k; j ++){
                for(int j1 = 0; j1 <= j; j1 ++){
                    int j2 = j - j1;
                    chmax(dp[u][j][1], pre_dp_u[j1][1] + dp[v][j2][1]);
                    chmax(dp[u][j][1], pre_f_u[j1] + f[v][j2 + 1]);
                    chmax(f[u][j], pre_f_u[j1] + dp[v][j2][1]);
                    chmax(f[u][j], (f[v][j1] + a[u]) + pre_dp_u[j2][0]);
                    chmax(dp[u][j][0], pre_dp_u[j1][0] + dp[v][j2][1]);
                }
            }
        }
        // for(int j = 0; j <= k; j ++){
        //     printf("dp[%d][%d][%d] = %lld\n", u, j, 0, dp[u][j][0]);
        //     printf("dp[%d][%d][%d] = %lld\n", u, j, 1, dp[u][j][1]);
        // }
    };
    DFS(1, 0);

    ll ans = 0;
    for(int i = 0; i <= k; i ++){
        ans = max(ans, dp[1][i][1]);
    }
    cout << ans << endl;
}


signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    // int T=1; cin>>T; while(T--)
    solve();
    return 0;
}

Submission Info

Submission Time
Task F - Paint Tree 2
User jjjxs
Language C++ 20 (gcc 12.2)
Score 525
Code Size 2776 Byte
Status AC
Exec Time 298 ms
Memory 153228 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 3
AC × 25
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 2 ms 3540 KiB
00_sample_01.txt AC 2 ms 3608 KiB
00_sample_02.txt AC 2 ms 3516 KiB
01_handmade_00.txt AC 2 ms 3660 KiB
01_handmade_01.txt AC 2 ms 3536 KiB
02_random_00.txt AC 27 ms 17584 KiB
02_random_01.txt AC 183 ms 91772 KiB
02_random_02.txt AC 45 ms 27232 KiB
02_random_03.txt AC 53 ms 31396 KiB
02_random_04.txt AC 191 ms 94956 KiB
02_random_05.txt AC 220 ms 115828 KiB
02_random_06.txt AC 232 ms 115944 KiB
02_random_07.txt AC 239 ms 115940 KiB
02_random_08.txt AC 239 ms 115828 KiB
02_random_09.txt AC 244 ms 115952 KiB
02_random_10.txt AC 257 ms 116236 KiB
02_random_11.txt AC 259 ms 116236 KiB
02_random_12.txt AC 204 ms 153080 KiB
02_random_13.txt AC 211 ms 153228 KiB
02_random_14.txt AC 296 ms 147044 KiB
02_random_15.txt AC 286 ms 146972 KiB
02_random_16.txt AC 279 ms 150796 KiB
02_random_17.txt AC 298 ms 150632 KiB
02_random_18.txt AC 201 ms 115688 KiB
02_random_19.txt AC 251 ms 115668 KiB