Submission #538036

#TimeUsernameProblemLanguageResultExecution timeMemory
538036zaneyuPaths (RMI21_paths)C++14
0 / 100
153 ms23912 KiB
/*input 3 3 1 2 3 2 3 1 */ #include<bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<n;i++) #define MNTO(x,y) x=min(x,y) #define MXTO(x,y) x=max(x,y) #define REP1(i,n) for(int i=1;i<=n;i++) #define ll long long #define ld long double #define sz(x) (int)x.size() #define ALL(x) x.begin(),x.end() #define pii pair<int,int> #define f first #define s second #define pb push_back const int maxn=1e5+5; vector<pii> v[maxn]; int dep[maxn]; ll dist[maxn]; int k; multiset<ll> ms; multiset<ll,greater<ll>> ls; ll s=0; void add(ll x){ ms.insert(x); s+=x; if(sz(ms)>k){ ll z=*ms.begin(); s-=z; ms.erase(ms.begin()); ls.insert(z); } } void remove(ll x){ auto it=ms.find(x); if(it==ms.end()){ ls.erase(ls.find(x)); } else{ s-=x; ms.erase(it); if(sz(ls)){ ll z=(*ls.begin()); s+=z; ms.insert(z); ls.erase(ls.begin()); } } } ll mx[maxn]; void dfs(int u,int p){ bool hv=0; for(auto x:v[u]){ if(x.f==p) continue; hv=1; dfs(x.f,u); remove(mx[x.f]); add(mx[x.f]+x.s); MXTO(mx[u],mx[x.f]+x.s); } add(0); } ll ans[maxn]; void dfs2(int u,int p){ vector<pii> asd; asd.pb({0,u}); for(auto x:v[u]){ asd.pb({mx[x.f]+x.s,x.f}); } ans[u]=s; sort(ALL(asd)),reverse(ALL(asd)); for(auto x:v[u]){ if(x.f==p) continue; ll tmp=mx[u]; mx[u]=asd[0].f; if(x.f==asd[0].s){ mx[u]=asd[1].f; } remove(mx[x.f]+x.s); add(mx[x.f]); add(mx[u]+x.s); remove(mx[u]); dfs2(x.f,u); remove(mx[x.f]); add(mx[x.f]+x.s); remove(mx[u]+x.s); add(mx[u]); mx[u]=tmp; } } int main(){ ios::sync_with_stdio(false),cin.tie(0); int n; cin>>n>>k; REP(i,n-1){ int a,b,c; cin>>a>>b>>c; --a,--b; v[a].pb({b,c}); v[b].pb({a,c}); } dfs(0,-1); dfs2(0,-1); REP(i,n) cout<<ans[i]<<'\n'; }

Compilation message (stderr)

Main.cpp: In function 'void dfs(int, int)':
Main.cpp:56:10: warning: variable 'hv' set but not used [-Wunused-but-set-variable]
   56 |     bool hv=0;
      |          ^~
#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...