# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
537782 | tqbfjotld | Paths (RMI21_paths) | C++14 | 317 ms | 19704 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |