#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
const int MAX = 3e5 + 5;
int n, k;
long long dp[MAX];
vector <pair <int, int>> adj[MAX];
namespace sub4 {
bool check() {
return n <= 2000;
}
void dfs(int u, int p, vector <long long> &res) {
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v = tmp.fi, w = tmp.se;
dfs(v, u, res);
dp[u] = max(dp[u], dp[v] + w);
}
bool oke = false;
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v = tmp.fi, w = tmp.se;
if(dp[u] != dp[v] + w || oke) res.emplace_back(dp[v] + w);
else oke = true;
}
}
void solve() {
for (int i = 1; i <= n; ++i) {
fill(dp + 1, dp + n + 1, 0);
vector <long long> res;
dfs(i, 0, res);
res.push_back(dp[i]);
sort(res.begin(), res.end(), greater <long long>());
long long sum = 0;
for (int j = 0; j < min((int) res.size(), k); ++j) sum += res[j];
cout << sum << '\n';
}
exit(0);
}
}
namespace sub_full {
bool check() {
return true;
}
multiset <long long> values, max_k;
long long total, ans[MAX];
pair <long long, long long> dp[MAX];
void insert(long long val) {
if(max_k.empty() || val > *max_k.begin()) {
total += val;
max_k.insert(val);
} else {
values.insert(val);
}
}
void remove(long long val) {
if(max_k.count(val)) {
max_k.erase(max_k.find(val));
total -= val;
} else {
if(!values.count(val)) return;
values.erase(values.find(val));
}
}
void get_ans() {
while((int) max_k.size() < k && !values.empty()) {
total += *values.rbegin();
max_k.insert(*values.rbegin());
values.erase(values.find(*values.rbegin()));
}
while((int) max_k.size() > k) {
total -= *max_k.begin();
values.insert(*max_k.begin());
max_k.erase(max_k.begin());
}
}
void dfs(int u, int p) {
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v = tmp.fi, w = tmp.se;
dfs(v, u);
if(dp[u].fi < dp[v].fi + w) {
dp[u].se = dp[u].fi;
dp[u].fi = dp[v].fi + w;
} else dp[u].se = max(dp[u].se, dp[v].fi + w);
}
bool used = false;
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v, w; tie(v, w) = tmp;
if(dp[u].fi != dp[v].fi + w || used) insert(dp[v].fi + w);
else used = true;
}
}
void calc(int u, int p) {
get_ans();
ans[u] = (k == 1 ? dp[u].fi : total);
for (pair <int, int> tmp : adj[u]) if(tmp.fi != p) {
int v, w; tie(v, w) = tmp;
long long val1 = dp[v].fi;
remove(val1 + w); insert(val1);
long long val2 = (dp[u].fi == dp[v].fi + w ? dp[u].se : dp[u].fi);
remove(val2); insert(val2 + w);
if(dp[v].fi < val2 + w) {
dp[v].se = dp[v].fi;
dp[v].fi = val2 + w;
} else dp[v].se = max(dp[v].se, val2 + w);
calc(v, u);
insert(val1 + w), remove(val1);
insert(val2), remove(val2 + w);
}
}
void solve() {
dfs(1, 0);
insert(dp[1].fi);
calc(1, 0);
for (int i = 1; i <= n; ++i) cout << ans[i] << "\n";
exit(0);
}
}
void you_make_it(void) {
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
if(sub_full::check()) sub_full::solve();
if(sub4::check()) sub4::solve();
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("paths");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:164:5: note: in expansion of macro 'file'
164 | file("paths");
| ^~~~
Main.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:164:5: note: in expansion of macro 'file'
164 | file("paths");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Correct |
3 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10736 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Correct |
3 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10736 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Correct |
3 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10736 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
13 |
Correct |
4 ms |
10844 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
5 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
4 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
19456 KB |
Output is correct |
2 |
Correct |
182 ms |
21588 KB |
Output is correct |
3 |
Correct |
94 ms |
17084 KB |
Output is correct |
4 |
Correct |
179 ms |
19480 KB |
Output is correct |
5 |
Correct |
162 ms |
20388 KB |
Output is correct |
6 |
Correct |
194 ms |
19668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10676 KB |
Output is correct |
3 |
Correct |
3 ms |
10584 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10736 KB |
Output is correct |
7 |
Correct |
3 ms |
10588 KB |
Output is correct |
8 |
Correct |
3 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10588 KB |
Output is correct |
11 |
Correct |
4 ms |
10588 KB |
Output is correct |
12 |
Correct |
3 ms |
10584 KB |
Output is correct |
13 |
Correct |
4 ms |
10844 KB |
Output is correct |
14 |
Correct |
4 ms |
10844 KB |
Output is correct |
15 |
Correct |
5 ms |
10844 KB |
Output is correct |
16 |
Correct |
4 ms |
10844 KB |
Output is correct |
17 |
Correct |
4 ms |
10844 KB |
Output is correct |
18 |
Correct |
4 ms |
10844 KB |
Output is correct |
19 |
Correct |
4 ms |
10840 KB |
Output is correct |
20 |
Correct |
166 ms |
19456 KB |
Output is correct |
21 |
Correct |
182 ms |
21588 KB |
Output is correct |
22 |
Correct |
94 ms |
17084 KB |
Output is correct |
23 |
Correct |
179 ms |
19480 KB |
Output is correct |
24 |
Correct |
162 ms |
20388 KB |
Output is correct |
25 |
Correct |
194 ms |
19668 KB |
Output is correct |
26 |
Correct |
195 ms |
21796 KB |
Output is correct |
27 |
Correct |
167 ms |
23580 KB |
Output is correct |
28 |
Correct |
162 ms |
23984 KB |
Output is correct |
29 |
Correct |
115 ms |
19364 KB |
Output is correct |
30 |
Correct |
164 ms |
21944 KB |
Output is correct |
31 |
Correct |
197 ms |
20252 KB |
Output is correct |
32 |
Correct |
180 ms |
22868 KB |
Output is correct |
33 |
Correct |
192 ms |
21912 KB |
Output is correct |
34 |
Correct |
92 ms |
19056 KB |
Output is correct |
35 |
Correct |
214 ms |
21860 KB |
Output is correct |
36 |
Correct |
163 ms |
23956 KB |
Output is correct |