# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537771 | 2022-03-15T13:36:12 Z | tqbfjotld | Paths (RMI21_paths) | C++14 | 289 ms | 18672 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{ 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; for (auto x : adjl[node]){ if (x.first==pa) continue; int chid; 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-1; 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 | Runtime error | 4 ms | 5204 KB | Execution killed with signal 6 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 4 ms | 5204 KB | Execution killed with signal 6 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 4 ms | 5204 KB | Execution killed with signal 6 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 4 ms | 5204 KB | Execution killed with signal 6 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 289 ms | 16252 KB | Output is correct |
2 | Correct | 266 ms | 18672 KB | Output is correct |
3 | Correct | 191 ms | 16184 KB | Output is correct |
4 | Correct | 236 ms | 16196 KB | Output is correct |
5 | Correct | 262 ms | 17384 KB | Output is correct |
6 | Correct | 235 ms | 16144 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 4 ms | 5204 KB | Execution killed with signal 6 |
2 | Halted | 0 ms | 0 KB | - |