#include <bits/stdc++.h>
#define int long long
#define ii pair <int, int>
#define F first
#define S second
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int n, k, dem;
vector <ii> c[N];
long long ans[N], dp1[N], dp2[N], a = 0;
multiset <long long> s, Maxk;
void add(long long val){
//cout << 1 << ' ' << val << '\n';
if (val >= *Maxk.begin()) Maxk.insert(val), a += val;
else s.insert(val);
}
void adv(){
while (Maxk.size() < k and s.size() >= 1){
a += *s.rbegin();
Maxk.insert(*s.rbegin());
s.erase(s.lower_bound(*s.rbegin()));
}
while (Maxk.size() > k){
a -= *Maxk.begin();
s.insert(*Maxk.begin());
Maxk.erase(Maxk.begin());
}
}
void del(long long val){
//cout << 0 << ' ' << val << '\n';
if (Maxk.count(val)) Maxk.erase(Maxk.lower_bound(val)), a -= val;
else s.erase(s.lower_bound(val));
}
void dfs(int u, int pa){
//add(0, ans[u]);
dp1[u] = 0, dp2[u] = 0;
for (ii v : c[u]){
if (v.F == pa) continue;
dfs(v.F, u);
if (dp1[u] < dp1[v.F] + v.S) dp2[u] = dp1[u], dp1[u] = dp1[v.F] + v.S;
else dp2[u] = max(dp2[u], dp1[v.F] + v.S);
}
bool used = false;
for (ii v : c[u]){
if (v.F == pa) continue;
if (used or dp1[v.F] + v.S < dp1[u] or u == 1) add(dp1[v.F] + v.S);
else used = true;
}
}
void get(int u, int pa){
adv();
ans[u] = a;
for (ii v : c[u]){
if (v.F == pa) continue;
//cout << v.F << endl;
int val1 = dp1[v.F] + v.S;
int val2 = dp1[v.F];
del(dp1[v.F] + v.S);
add(dp1[v.F]);
//if (v.F == 4) cout << -111111 << endl;
long long val = dp1[u];
if (val == dp1[v.F] + v.S) val = dp2[u];
add(val + v.S);
del(val);
//cout << val << endl;
if (dp1[v.F] < val + v.S){
dp2[v.F] = dp1[v.F];
dp1[v.F] = val + v.S;
}
else dp2[v.F] = max(dp2[v.F], val + v.S);
get(v.F, u);
long long kk = 0;
add(val1);
del(val2);
add(val);
del(val + v.S);
}
}
void solve(){
cin >> n >> k;
for (int i = 1; i <= n - 1; ++ i){
int u, v, w;
cin >> u >> v >> w;
c[u].push_back(ii(v, w));
c[v].push_back(ii(u, w));
}
dfs(1, 0);
get(1, 0);
if (k == 1) for (int i = 1; i <= n; ++ i) cout << dp1[i] << '\n';
else for (int i = 1; i <= n; ++ i) cout << ans[i] << '\n';
}
signed main(){
//freopen("code.inp", "r", stdin);
//freopen("code.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t --) solve();
return 0;
}
Compilation message
Main.cpp: In function 'void adv()':
Main.cpp:24:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
24 | while (Maxk.size() < k and s.size() >= 1){
| ~~~~~~~~~~~~^~~
Main.cpp:29:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
29 | while (Maxk.size() > k){
| ~~~~~~~~~~~~^~~
Main.cpp: In function 'void get(long long int, long long int)':
Main.cpp:83:19: warning: unused variable 'kk' [-Wunused-variable]
83 | long long kk = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
13636 KB |
Output is correct |
2 |
Correct |
127 ms |
18120 KB |
Output is correct |
3 |
Correct |
73 ms |
13768 KB |
Output is correct |
4 |
Correct |
120 ms |
15800 KB |
Output is correct |
5 |
Correct |
127 ms |
16772 KB |
Output is correct |
6 |
Correct |
125 ms |
15820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
1 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
3 ms |
4952 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
116 ms |
13636 KB |
Output is correct |
21 |
Correct |
127 ms |
18120 KB |
Output is correct |
22 |
Correct |
73 ms |
13768 KB |
Output is correct |
23 |
Correct |
120 ms |
15800 KB |
Output is correct |
24 |
Correct |
127 ms |
16772 KB |
Output is correct |
25 |
Correct |
125 ms |
15820 KB |
Output is correct |
26 |
Correct |
139 ms |
16468 KB |
Output is correct |
27 |
Correct |
145 ms |
18004 KB |
Output is correct |
28 |
Correct |
113 ms |
18772 KB |
Output is correct |
29 |
Correct |
85 ms |
13856 KB |
Output is correct |
30 |
Correct |
147 ms |
16180 KB |
Output is correct |
31 |
Correct |
121 ms |
14600 KB |
Output is correct |
32 |
Correct |
127 ms |
16992 KB |
Output is correct |
33 |
Correct |
126 ms |
16468 KB |
Output is correct |
34 |
Correct |
76 ms |
13648 KB |
Output is correct |
35 |
Correct |
129 ms |
16076 KB |
Output is correct |
36 |
Correct |
121 ms |
18724 KB |
Output is correct |