31pts,下载错误测试用例和答案相同,交上去WA,求调
查看原帖
31pts,下载错误测试用例和答案相同,交上去WA,求调
660553
resautu楼主2024/3/16 11:16
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
vector<pair<int, int>> adj[N];
int n, q;
int dp[N][N];
int childs[N], sums[N];
int w[N];
void dfs(int cur, int p, int val){
    childs[cur] = 1;
    w[cur] = sums[cur] = val;
    int c1 = -1, c2 = -1;
    for(auto &ele : adj[cur]){
        if(ele.first != p){
            dfs(ele.first, cur, ele.second);
            childs[cur] += childs[ele.first];
            sums[cur] += sums[ele.first];
            if(c1 == -1) c1 = ele.first;
            else c2 = ele.first;
        }
    }
    dp[cur][childs[cur]] = 0;
    dp[cur][0] = sums[cur];
    for(int i = 0; i <= childs[c1]; i++){
        for(int j = 0; j <= childs[c2]; j++){
            dp[cur][i + j] = max(dp[cur][i + j], dp[c1][i] + dp[c2][j] + w[cur]);
        }
    }
}

int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int s, f, w;
    cin >> n >> q;
    memset(dp, -0x3f, sizeof dp);
    for(int i = 0; i < n - 1; i++){
        cin >> s >> f >> w;
        adj[s].push_back({f, w});
        adj[f].push_back({s, w});
    }

    dfs(1, 0, 0);
    cout << dp[1][n - q - 1] << endl;
    return 0;
}

2024/3/16 11:16
加载中...