Submission #509719

#TimeUsernameProblemLanguageResultExecution timeMemory
509719RainbowbunnyPaths (RMI21_paths)C++17
56 / 100
1018 ms10180 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 1e5 + 5;

int n, k;
ll mx[MAXN];
pair <ll, int> dp[MAXN];
vector <pair <int, ll> > Adj[MAXN];

void DFS(int node, int p = -1)
{
    for(auto x : Adj[node])
    {
        if(x.first == p)
        {
            continue;
        }
        DFS(x.first, node);
        dp[x.first].first += x.second;
        dp[node] = max(dp[node], dp[x.first]);
    }
    if(dp[node].second == 0)
    {
        dp[node] = make_pair(0, node);
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    if(n > MAXN)
    {
        return 0;
    }
    for(int i = 1; i < n; i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        Adj[u].emplace_back(v, c);
        Adj[v].emplace_back(u, c);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            mx[j] = 0;
            dp[j] = make_pair(0, 0);
        }
        DFS(i);
        for(int j = 1; j <= n; j++)
        {
            mx[dp[j].second] = max(mx[dp[j].second], dp[j].first);
        }
        sort(mx + 1, mx + n + 1, greater <ll> ());
        ll ans = 0;
        for(int j = 1; j <= k; j++)
        {
            ans += mx[j];
        }
        cout << ans << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...