# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537782 | 2022-03-15T13:46:27 Z | tqbfjotld | Paths (RMI21_paths) | C++14 | 317 ms | 19704 KB |
#include <bits/stdc++.h> using namespace std; #define int long long int chainlengths[100005]; int curans = 0; int ch[100005]; int ans[100005]; vector<pair<int,int> > adjl[100005]; int n,K; multiset<int> s1,s2; void add(int num){ //printf("add %lld\n",num); if (s2.empty() || num>(*--s2.end())){ s1.insert(num); curans += num; } else{ s2.insert(num); } if (s1.size()>K){ s2.insert(*s1.begin()); curans -= *s1.begin(); s1.erase(s1.begin()); } } void rem(int num){ //printf("rem %lld\n",num); auto it = s1.lower_bound(num); if (it!=s1.end() && (*it)==num){ s1.erase(s1.lower_bound(num)); curans -= num; } else{ assert((*s2.lower_bound(num))==num); s2.erase(s2.lower_bound(num)); } if (s1.size()<K && s2.size()>0){ curans += *--s2.end(); s1.insert(*--s2.end()); s2.erase(--s2.end()); } } pair<int,int> dfs1(int node, int pa){ int cch = node; int cval = 0; for (auto x : adjl[node]){ if (x.first==pa){ continue; } auto res = dfs1(x.first,node); res.first += x.second; chainlengths[res.second] = res.first; if (res.first>cval){ cch = res.second; cval = res.first; } } ch[node] = cch; return {cval,cch}; } void dfs2(int node, int pa){ int orig = ch[node]; int l1 = 0; int l2 = 0; int l1n = node; int l2n = node; for (auto x : adjl[node]){ if (chainlengths[ch[x.first]]>=l1){ l2 = l1; l2n = l1n; l1 = chainlengths[ch[x.first]]; l1n = x.first; } else if (chainlengths[ch[x.first]]>=l2){ l2 = chainlengths[ch[x.first]]; l2n = x.first; } } ans[node] = curans; assert(s1.size()<=K); for (auto x : adjl[node]){ if (x.first==pa) continue; int chid; if (adjl[node].size()==1){ chid = node; } else if (x.first==l1n){ chid = ch[l2n]; } else{ chid = ch[l1n]; } rem(chainlengths[chid]); chainlengths[chid]+=x.second; ch[node] = chid; add(chainlengths[chid]); rem(chainlengths[ch[x.first]]); chainlengths[ch[x.first]] -= x.second; add(chainlengths[ch[x.first]]); dfs2(x.first,node); rem(chainlengths[chid]); chainlengths[chid]-=x.second; add(chainlengths[chid]); rem(chainlengths[ch[x.first]]); chainlengths[ch[x.first]] += x.second; add(chainlengths[ch[x.first]]); } ch[node] = orig; } main(){ scanf("%lld%lld",&n,&K); for (int x = 0; x<n-1; x++){ int a,b,c; scanf("%lld%lld%lld",&a,&b,&c); adjl[a].push_back({b,c}); adjl[b].push_back({a,c}); } auto res1 = dfs1(1,0); for (int x = 0; x<n; x++){ add(chainlengths[x+1]); } dfs2(1,0); for (int x = 1; x<=n; x++){ printf("%lld\n",ans[x]); } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 2644 KB | Output is correct |
2 | Correct | 2 ms | 2644 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 2644 KB | Output is correct |
2 | Correct | 2 ms | 2644 KB | Output is correct |
3 | Correct | 2 ms | 2644 KB | Output is correct |
4 | Correct | 2 ms | 2644 KB | Output is correct |
5 | Correct | 2 ms | 2644 KB | Output is correct |
6 | Correct | 2 ms | 2644 KB | Output is correct |
7 | Correct | 2 ms | 2644 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 2644 KB | Output is correct |
2 | Correct | 2 ms | 2644 KB | Output is correct |
3 | Correct | 2 ms | 2644 KB | Output is correct |
4 | Correct | 2 ms | 2644 KB | Output is correct |
5 | Correct | 2 ms | 2644 KB | Output is correct |
6 | Correct | 2 ms | 2644 KB | Output is correct |
7 | Correct | 2 ms | 2644 KB | Output is correct |
8 | Correct | 3 ms | 2772 KB | Output is correct |
9 | Correct | 3 ms | 2772 KB | Output is correct |
10 | Correct | 4 ms | 2772 KB | Output is correct |
11 | Correct | 3 ms | 2796 KB | Output is correct |
12 | Correct | 3 ms | 2772 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 2644 KB | Output is correct |
2 | Correct | 2 ms | 2644 KB | Output is correct |
3 | Correct | 2 ms | 2644 KB | Output is correct |
4 | Correct | 2 ms | 2644 KB | Output is correct |
5 | Correct | 2 ms | 2644 KB | Output is correct |
6 | Correct | 2 ms | 2644 KB | Output is correct |
7 | Correct | 2 ms | 2644 KB | Output is correct |
8 | Correct | 3 ms | 2772 KB | Output is correct |
9 | Correct | 3 ms | 2772 KB | Output is correct |
10 | Correct | 4 ms | 2772 KB | Output is correct |
11 | Correct | 3 ms | 2796 KB | Output is correct |
12 | Correct | 3 ms | 2772 KB | Output is correct |
13 | Correct | 6 ms | 2900 KB | Output is correct |
14 | Correct | 5 ms | 3052 KB | Output is correct |
15 | Correct | 5 ms | 2984 KB | Output is correct |
16 | Correct | 5 ms | 2856 KB | Output is correct |
17 | Correct | 5 ms | 2900 KB | Output is correct |
18 | Correct | 4 ms | 2900 KB | Output is correct |
19 | Correct | 5 ms | 2900 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 268 ms | 15872 KB | Output is correct |
2 | Correct | 262 ms | 18392 KB | Output is correct |
3 | Correct | 202 ms | 15700 KB | Output is correct |
4 | Correct | 275 ms | 15752 KB | Output is correct |
5 | Correct | 263 ms | 16956 KB | Output is correct |
6 | Correct | 281 ms | 15664 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 2644 KB | Output is correct |
2 | Correct | 2 ms | 2644 KB | Output is correct |
3 | Correct | 2 ms | 2644 KB | Output is correct |
4 | Correct | 2 ms | 2644 KB | Output is correct |
5 | Correct | 2 ms | 2644 KB | Output is correct |
6 | Correct | 2 ms | 2644 KB | Output is correct |
7 | Correct | 2 ms | 2644 KB | Output is correct |
8 | Correct | 3 ms | 2772 KB | Output is correct |
9 | Correct | 3 ms | 2772 KB | Output is correct |
10 | Correct | 4 ms | 2772 KB | Output is correct |
11 | Correct | 3 ms | 2796 KB | Output is correct |
12 | Correct | 3 ms | 2772 KB | Output is correct |
13 | Correct | 6 ms | 2900 KB | Output is correct |
14 | Correct | 5 ms | 3052 KB | Output is correct |
15 | Correct | 5 ms | 2984 KB | Output is correct |
16 | Correct | 5 ms | 2856 KB | Output is correct |
17 | Correct | 5 ms | 2900 KB | Output is correct |
18 | Correct | 4 ms | 2900 KB | Output is correct |
19 | Correct | 5 ms | 2900 KB | Output is correct |
20 | Correct | 268 ms | 15872 KB | Output is correct |
21 | Correct | 262 ms | 18392 KB | Output is correct |
22 | Correct | 202 ms | 15700 KB | Output is correct |
23 | Correct | 275 ms | 15752 KB | Output is correct |
24 | Correct | 263 ms | 16956 KB | Output is correct |
25 | Correct | 281 ms | 15664 KB | Output is correct |
26 | Correct | 317 ms | 16608 KB | Output is correct |
27 | Correct | 271 ms | 18720 KB | Output is correct |
28 | Correct | 283 ms | 19240 KB | Output is correct |
29 | Correct | 212 ms | 16492 KB | Output is correct |
30 | Correct | 289 ms | 16444 KB | Output is correct |
31 | Correct | 297 ms | 16528 KB | Output is correct |
32 | Correct | 291 ms | 17640 KB | Output is correct |
33 | Correct | 303 ms | 16576 KB | Output is correct |
34 | Correct | 183 ms | 16356 KB | Output is correct |
35 | Correct | 283 ms | 16476 KB | Output is correct |
36 | Correct | 278 ms | 19704 KB | Output is correct |