Submission #348073

#TimeUsernameProblemLanguageResultExecution timeMemory
348073arnold518Swapping Cities (APIO20_swap)C++14
100 / 100
608 ms56808 KiB
#include "swap.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 1e5; const int MAXM = 2e5; int N, M; struct Edge { int u, v, w; }; Edge E[MAXM+10]; vector<int> V[MAXN+10]; int deg[MAXN+10], val[MAXN+10], cnt[MAXN+10], sz[MAXN+10]; int ans[MAXN+10]; vector<pii> adj[MAXN+10]; struct UF { int par[MAXN+10]; void init() { for(int i=1; i<=N; i++) { par[i]=i; } } int Find(int x) { if(x==par[x]) return x; return par[x]=Find(par[x]); } }uf; int par[MAXN+10][30], len[MAXN+10][30], dep[MAXN+10]; void dfs(int now, int bef, int d) { par[now][0]=bef; dep[now]=d; for(auto nxt : adj[now]) { if(nxt.first==bef) continue; len[nxt.first][0]=nxt.second; dfs(nxt.first, now, d+1); } } int lca(int u, int v) { if(dep[u]>dep[v]) swap(u, v); int ans=0; for(int i=20; i>=0; i--) if(dep[par[v][i]]>=dep[u]) { ans=max(ans, len[v][i]); v=par[v][i]; } if(u==v) return ans; for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) { ans=max(ans, len[v][i]); ans=max(ans, len[u][i]); u=par[u][i]; v=par[v][i]; } ans=max(ans, len[u][0]); ans=max(ans, len[v][0]); return ans; } void init(int _N, int _M, vector<int> _U, vector<int> _V, vector<int> _W) { N=_N; M=_M; for(int i=1; i<=M; i++) { int u=_U[i-1]+1, v=_V[i-1]+1, w=_W[i-1]; E[i]={u, v, w}; } sort(E+1, E+M+1, [&](const Edge &p, const Edge &q) { return p.w<q.w; }); for(int i=1; i<=N; i++) ans[i]=2e9; uf.init(); for(int i=1; i<=N; i++) { V[i].push_back(i); deg[i]=0; val[i]=0; cnt[i]=0; sz[i]=1; } for(int i=1; i<=M; i++) { int u=E[i].u, v=E[i].v, w=E[i].w; deg[u]++; deg[v]++; int uu=uf.Find(u), vv=uf.Find(v); if(uu==vv) { val[uu]=max(val[uu], deg[u]); val[uu]=max(val[uu], deg[v]); cnt[uu]++; } else { adj[u].push_back({v, w}); adj[v].push_back({u, w}); if(V[uu].size()<V[vv].size()) swap(V[uu], V[vv]); for(auto it : V[vv]) V[uu].push_back(it); V[vv].clear(); uf.par[vv]=uu; val[uu]=max(val[uu], val[vv]); cnt[uu]+=cnt[vv]; sz[uu]+=sz[vv]; val[uu]=max(val[uu], deg[u]); val[uu]=max(val[uu], deg[v]); cnt[uu]++; } bool t=false; if(val[uu]>=3) t=true; if(val[uu]==2 && cnt[uu]==sz[uu]) t=true; if(t) { for(auto it : V[uu]) ans[it]=w; V[uu].clear(); } } dfs(1, 0, 1); for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) { par[j][i]=par[par[j][i-1]][i-1]; len[j][i]=max(len[j][i-1], len[par[j][i-1]][i-1]); } } int getMinimumFuelCapacity(int X, int Y) { X++; Y++; int aans=lca(X, Y); aans=max(aans, ans[X]); aans=max(aans, ans[Y]); if(aans>=2e9) aans=-1; return aans; }
#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...