Submission #574066

#TimeUsernameProblemLanguageResultExecution timeMemory
574066uroskPaths (RMI21_paths)C++14
56 / 100
1086 ms42004 KiB
//https://oj.uz/problem/view/RMI21_paths #define here cerr<<"===========================================\n" #include <bits/stdc++.h> #define ld double #define ll long long #define llinf 100000000000000000LL // 10^17 #define pb push_back #define popb pop_back #define fi first #define sc second #define endl '\n' #define pll pair<ll,ll> #define pld pair<ld,ld> #define sz(a) (ll)(a.size()) #define all(a) a.begin(),a.end() #define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;} #define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0); using namespace std; #define maxn 1000005 #define lg 13 ll n,k; vector<pll> g[maxn]; ll ans[maxn]; ll dp[maxn]; ll st[maxn][lg]; ll val[maxn]; ll sum[maxn]; vector<ll> v; void dfs(ll u,ll par){ st[u][0] = par; dp[u] = u; for(pll p : g[u]){ ll s = p.fi; ll w = p.sc; if(s==par) continue; sum[s] = sum[u] + w; dfs(s,u); if(sum[dp[u]] - sum[u]<sum[dp[s]] - sum[s] + w) dp[u] = dp[s]; } } bool cmp(ll x,ll y){return val[x]>val[y];} void reshi(ll rut){ sum[rut] = 0; dfs(rut,rut); for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[i][j] = st[st[i][j-1]][j-1]; for(ll i = 1;i<=n;i++){ ll e = 0; ll cur = i; for(ll j = lg-1;j>=0;j--){ if(dp[st[cur][j]]==i) cur = st[cur][j]; } val[i] = sum[i] - sum[st[cur][0]]; } sort(all(v),cmp); for(ll i = 1;i<=k;i++) ans[rut]+=val[v[i-1]]; } int main(){ daj_mi_malo_vremena cin >> n >> k; ll sve = 0; for(ll i = 1;i<=n-1;i++){ ll x,y,w; cin >> x >> y >> w; g[x].pb({y,w}); g[y].pb({x,w}); sve+=w; } for(ll i = 1;i<=n;i++) if(sz(g[i])==1) v.pb(i); if(sz(v)<=k){ for(ll i = 1;i<=n;i++) cout<<sve<<endl; return 0; } for(ll i = 1;i<=n;i++) reshi(i); for(ll i = 1;i<=n;i++) cout<<ans[i]<<endl; return 0; }

Compilation message (stderr)

Main.cpp: In function 'void reshi(long long int)':
Main.cpp:49:12: warning: unused variable 'e' [-Wunused-variable]
   49 |         ll e = 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...