Submission #538732

#TimeUsernameProblemLanguageResultExecution timeMemory
538732dantoh000Paths (RMI21_paths)C++14
100 / 100
305 ms16796 KiB
#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; typedef pair<int,int> ii; int n,k; vector<ii> G[100005]; ll d[100005]; int p[100005]; int dep[100005]; int d1 = 0; int d2 = 0; int vis[19]; int dtp[100005]; int ondia[100005]; ll len[100005]; multiset<ll> top; multiset<ll> bottom; ll cursum = 0; ll ans[100005]; void change(ll a, ll b){ //printf("changing %lld for %lld\n",a,b); if (top.find(a) != top.end()) { top.erase(top.find(a)); cursum -= a; } else if (bottom.find(a) != bottom.end()) bottom.erase(bottom.find(a)); if (top.empty() || b >= *top.begin()){ top.insert(b); cursum += b; } else{ bottom.insert(b); } while (top.size() > k){ auto it = top.begin(); bottom.insert(*it); //printf("too many, removing %lld\n",*it); cursum -= *it; top.erase(it); } while (bottom.size() && top.size() < k){ auto it = --bottom.end(); top.insert(*it); //printf("too little, adding %lld\n",*it); cursum += *it; bottom.erase(it); } } void dfs(int u){ for (auto v : G[u]){ if (v.fi == p[u]) continue; d[v.fi] = d[u]+v.se; dep[v.fi] = dep[u]+1; p[v.fi] = u; dtp[v.fi] = v.se; dfs(v.fi); } } ll dfs2(int u){ ll best = 0; for (auto v : G[u]){ if (v.fi == p[u]) continue; ll x = dfs2(v.fi); if (best < x){ if (best != 0) change(-1, best); best = x; } else{ if (x != 0) change(-1, x); } } len[u] = best+dtp[u]; //printf("len from %d = %lld\n",u,len[u]); return len[u]; } void dfs3(int u, ll l){ //printf("currently at %d: ans = %lld\n",u,cursum); //printf("at top: "); for (auto x : top) printf("%lld ",x); printf("\n"); //printf("at bottom: "); for (auto x : bottom) printf("%lld ",x); printf("\n"); ans[u] = cursum; for (auto v : G[u]){ if (v.fi == p[u]) continue; if (ondia[u] && !ondia[v.fi]){ l = max(d[d2]-d[u], d[u]); change(l,l+v.se); change(len[v.fi], len[v.fi]-v.se); dfs3(v.fi, l+v.se); change(len[v.fi]-v.se,len[v.fi]); change(l+v.se,l); } else if (!ondia[u] && !ondia[v.fi]){ change(l,l+v.se); change(len[v.fi], len[v.fi]-v.se); dfs3(v.fi, l+v.se); change(len[v.fi]-v.se,len[v.fi]); change(l+v.se,l); } else{ change(d[d2]-d[u], d[d2]-d[v.fi]); change(d[u], d[v.fi]); dfs3(v.fi,l); change(d[v.fi],d[u]); change(d[d2]-d[v.fi],d[d2]-d[u]); } } } int main(){ scanf("%d%d",&n,&k); for (int i = 1 ; i < n; i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); G[a].push_back({b,c}); G[b].push_back({a,c}); } memset(p,-1,sizeof(p)); dfs(1); for (int i = 1; i <= n; i++){ if (d[i] > d[d1]) d1 = i; } memset(p,-1,sizeof(p)); memset(d,0,sizeof(d)); memset(dep,0,sizeof(dep)); memset(dtp,0,sizeof(dtp)); dfs(d1); for (int i = 1; i <= n; i++){ if (d[i] > d[d2]) d2 = i; } //printf("diameter %d %d\n",d1,d2); int cur = d2; while (cur != -1){ ondia[cur] = 1; cur = p[cur]; } dfs2(d1); if (len[d1] != 0) change(-1, len[d1]); dfs3(d1, 0); for (int i = 1; i <= n; i++){ printf("%lld\n",ans[i]); } }

Compilation message (stderr)

Main.cpp: In function 'void change(ll, ll)':
Main.cpp:36:23: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |     while (top.size() > k){
      |            ~~~~~~~~~~~^~~
Main.cpp:43:40: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   43 |     while (bottom.size() && top.size() < k){
      |                             ~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  115 |     scanf("%d%d",&n,&k);
      |     ~~~~~^~~~~~~~~~~~~~
Main.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         scanf("%d%d%d",&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...