Submission #537782

#TimeUsernameProblemLanguageResultExecution timeMemory
537782tqbfjotldPaths (RMI21_paths)C++14
100 / 100
317 ms19704 KiB
#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)

Main.cpp: In function 'void add(long long int)':
Main.cpp:25:18: 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]
   25 |     if (s1.size()>K){
      |         ~~~~~~~~~^~
Main.cpp: In function 'void rem(long long int)':
Main.cpp:43:18: 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]
   43 |     if (s1.size()<K && s2.size()>0){
      |         ~~~~~~~~~^~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from Main.cpp:1:
Main.cpp: In function 'void dfs2(long long int, long long int)':
Main.cpp:88:21: 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]
   88 |     assert(s1.size()<=K);
      |            ~~~~~~~~~^~~
Main.cpp: At global scope:
Main.cpp:119:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  119 |  main(){
      |  ^~~~
Main.cpp: In function 'int main()':
Main.cpp:127:10: warning: variable 'res1' set but not used [-Wunused-but-set-variable]
  127 |     auto res1 = dfs1(1,0);
      |          ^~~~
Main.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |     scanf("%lld%lld",&n,&K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:123:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         scanf("%lld%lld%lld",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...