#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e18;
vector<pii> node[N];
vector<int> suff[N];
vector<int> operator+(vector<int> a,vector<int> b){
a.insert(a.end(),b.begin(),b.end());
return a;
}
bool asd;
void dfs1(int i,int p){
// to calc the suff
if (sz(node[i])==1&&p!=-1){
suff[i] = {0};
return;
}
for (auto [it,w] : node[i]){
if (it==p) continue;
dfs1(it,i);
suff[it].front() += w;
suff[i] = suff[i]+suff[it];
suff[it].front() -= w;
}
sort(all(suff[i]),greater<int>());
if (asd) suff[i].resize(2);
}
int ans[N];
int n,k;
void dfs2(int i,int p,vector<int> cur){
// calc the answer
// fix the cur for the next child
// idk
vector<int> tmp = cur + suff[i];
sort(all(tmp),greater<int>());
if (asd) tmp.resize(2);
for (int j=0;j<k;j++){
ans[i] += tmp[j];
}
for (auto [it1,w1] : node[i]){
if (it1==p) continue;
tmp = cur;
for (auto [it2,w2] : node[i]){
if (it2!=p&&it2!=it1){
suff[it2].front() += w2;
tmp = tmp + suff[it2];
suff[it2].front() -= w2;
}
}
sort(all(tmp),greater<int>());
tmp.front() += w1;
dfs2(it1,i,tmp);
}
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> k;
if (k==1) asd = 1;
for (int i=0;i<n-1;i++){
int u,v,w;
cin >> u >> v >> w;
node[u].pb({v,w});
node[v].pb({u,w});
}
dfs1(1,-1);
dfs2(1,-1,{});
for (int i=1;i<=n;i++){
cout << ans[i] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47188 KB |
Output is correct |
3 |
Correct |
24 ms |
47332 KB |
Output is correct |
4 |
Correct |
27 ms |
47444 KB |
Output is correct |
5 |
Correct |
25 ms |
47364 KB |
Output is correct |
6 |
Correct |
23 ms |
47272 KB |
Output is correct |
7 |
Correct |
24 ms |
47316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47188 KB |
Output is correct |
3 |
Correct |
24 ms |
47332 KB |
Output is correct |
4 |
Correct |
27 ms |
47444 KB |
Output is correct |
5 |
Correct |
25 ms |
47364 KB |
Output is correct |
6 |
Correct |
23 ms |
47272 KB |
Output is correct |
7 |
Correct |
24 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
42 ms |
47572 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47188 KB |
Output is correct |
3 |
Correct |
24 ms |
47332 KB |
Output is correct |
4 |
Correct |
27 ms |
47444 KB |
Output is correct |
5 |
Correct |
25 ms |
47364 KB |
Output is correct |
6 |
Correct |
23 ms |
47272 KB |
Output is correct |
7 |
Correct |
24 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
42 ms |
47572 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
849 ms |
58584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47188 KB |
Output is correct |
3 |
Correct |
24 ms |
47332 KB |
Output is correct |
4 |
Correct |
27 ms |
47444 KB |
Output is correct |
5 |
Correct |
25 ms |
47364 KB |
Output is correct |
6 |
Correct |
23 ms |
47272 KB |
Output is correct |
7 |
Correct |
24 ms |
47316 KB |
Output is correct |
8 |
Incorrect |
42 ms |
47572 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |