Submission #561473

#TimeUsernameProblemLanguageResultExecution timeMemory
561473uroskDreaming (IOI13_dreaming)C++14
100 / 100
78 ms16756 KiB
#include "dreaming.h" #define here cerr<<"===========================================\n" #include <bits/stdc++.h> #define ld double #define ll int #define ull unsigned long long #define llinf 100000000000000000LL // 10^17 #define iinf 2000000000 // 2*10^9 #define pb push_back #define popb pop_back #define fi first #define sc second #define endl '\n' #define pii pair<int,int> #define pll pair<ll,ll> #define pld pair<ld,ld> #define sz(a) int(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;} using namespace std; #define maxn 100005 ll n,m,l; vector<pll> g[maxn]; ll dp[maxn]; ll up[maxn]; pll dpmx[maxn]; bool vis[maxn]; ll dsu[maxn]; ll root(ll x){ while(x!=dsu[x]){ dsu[x] = dsu[dsu[x]]; x = dsu[x]; } return x; } void upd(ll x,ll y){ x = root(x); y = root(y); if(dp[x]>dp[y]) dsu[x] = y; else dsu[y] = x; } void dfs(ll u,ll par){ vis[u] = 1; dp[u] = 0; dpmx[u] = {0,0}; for(pll p : g[u]){ ll s = p.fi; ll w = p.sc; if(s==par) continue; dfs(s,u); dp[u] = max(dp[s]+w,dp[u]); if(dp[s]+w>dpmx[u].fi){ dpmx[u].sc = dpmx[u].fi; dpmx[u].fi = dp[s]+w; }else if(dp[s]+w>dpmx[u].sc){ dpmx[u].sc = dp[s]+w; } } } void dfs2(ll u,ll par,ll w){ if(u!=par){ up[u] = up[par]+w; pll p = dpmx[par]; ll x; if(dp[u]+w==p.fi) x = p.sc; else x = p.fi; up[u] = max(up[u],x+w); } vis[u] = 1; for(pll p : g[u]){ ll s = p.fi; ll w = p.sc; if(s==par) continue; dfs2(s,u,w); } } void dfs3(ll u,ll par){ vis[u] = 1; for(pll p : g[u]){ ll s = p.fi; ll w = p.sc; if(s==par) continue; upd(s,u); dfs3(s,u); } } int travelTime(int N, int M, int L, int A[], int B[], int T[]) { n = N; m = M; l = L; iota(dsu+1,dsu+n+1,1); for(ll i = 0;i<m;i++){ ll x = A[i]; ll y = B[i]; ll w = T[i]; x++; y++; g[x].pb({y,w}); g[y].pb({x,w}); } for(ll i = 1;i<=n;i++) if(!vis[i]) dfs(i,i); fill(vis,vis+n+1,0); for(ll i = 1;i<=n;i++) if(!vis[i]) dfs2(i,i,0); for(ll i = 1;i<=n;i++) dp[i] = max(dp[i],up[i]); fill(vis,vis+n+1,0); for(ll i = 1;i<=n;i++) if(!vis[i]) dfs3(i,i); ll ans = 0; for(ll i = 1;i<=n;i++) ans = max(ans,dp[i]); fill(vis,vis+n+1,0); priority_queue<ll> pq; for(ll i = 1;i<=n;i++){ ll x = root(i); if(vis[x]) continue; pq.push(dp[x]); vis[x] = 1; } while(sz(pq)>=2){ ll x = pq.top(); pq.pop(); ll y = pq.top(); pq.pop(); ll z = min(max(x+l,y),max(y+l,x)); ans = max(ans,x+y+l); pq.push(z); } if(sz(pq)) ans = max(ans,pq.top()); return ans; }

Compilation message (stderr)

dreaming.cpp: In function 'void dfs3(int, int)':
dreaming.cpp:81:12: warning: unused variable 'w' [-Wunused-variable]
   81 |         ll w = p.sc;
      |            ^
#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...