This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
typedef long long int lli;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
template<class x>
using ordered_set = tree<x, null_type,less<x>, rb_tree_tag,tree_order_statistics_node_update>;
ll mod=(ll)1e9+7;
ll mod1=998244353;
///the defines :)
#define endl '\n'
#define vi vector<int>
#define vll vector<ll>
#define ent(arr) for(int i=0;i<arr.size();i++)cin>>arr[i];
#define all(arr) arr.begin(),arr.end()
#define allr(arr) arr.rbegin(),arr.rend()
#define sz size()
#define int long long
vector<vector<pair<int,int>>>adj;
int length[20002];
pair<int,int>dfs(int node,int parent,int w)
{
// cout<<"HI"<<endl;
pair<int,int>xd={0,0};
for(auto i:adj[node])
{
if(i.first==parent)
continue;
pair<int,int>mx=dfs(i.first,node,i.second);
mx.first+=w;
xd=max(xd,mx);
}
if(adj[node].sz==1)
{
length[node]=w;
return {w,node};
}
length[xd.second]+=w;
return xd;
}
void preprocess() {}
void solve()
{
int n,k;
cin>>n>>k;
adj.resize(n+1);
for(int i=0;i<n-1;i++)
{
int a,b,c;
cin>>a>>b>>c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
for(int i=1;i<=n;i++)
{
//cout<<i<<endl;
for(int j=1;j<=n;j++)length[j]=0;
dfs(i,0,0);
vi xd;
for(int j=1;j<=n;j++)xd.push_back(length[j]);
sort(allr(xd));
int ans=0;
for(int j=0;j<min((int)xd.sz,k);j++)
ans+=xd[j];
cout<<ans<<endl;
}
}
signed main()
{
// freopen("meta_game_input.txt","r",stdin);
// freopen("otput.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
preprocess();
//bla();
int t=1;
//cin>>t;
while(t--)
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |