Submission #293656

#TimeUsernameProblemLanguageResultExecution timeMemory
293656errorgornSwapping Cities (APIO20_swap)C++14
100 / 100
474 ms56988 KiB
#include "swap.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define ii pair<ll,ll> #define iii pair<ll,ii> #define fi first #define se second #define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--)) #define sz(x) (int)(x).size() #define all(x) (x).begin(),(x).end() const int INF=1e9+100; int n,m; vector<ii> al[100005]; vector<iii> edges,loop; int p[200005]; //ufds int find(int i){ if (i==p[i]) return i; else return p[i]=find(p[i]); } ll w[200005]; //weight of kruskal tree ll extra[200005]; //extra stuff (fork, loop) int pa[200005][20]; //2k decomp on kruskal tree int d[200005]; //depth for lca query vector<int> child[200005]; //stupid al int IDX; int mov(int i,int k){ rep(x,0,20) if (k&(1<<x)) { i=pa[i][x]; } return i; } int lca(int i,int j){ if (d[i]<d[j]) swap(i,j); i=mov(i,d[i]-d[j]); if (i==j) return i; rep(x,20,0){ if (pa[i][x]!=pa[j][x]){ i=pa[i][x]; j=pa[j][x]; } } return pa[i][0]; } void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) { rep(x,0,200005) p[x]=x; memset(pa,-1,sizeof(pa)); memset(extra,63,sizeof(extra)); n=N,m=M; rep(x,0,m){ al[U[x]].push_back(ii(V[x],W[x])); al[V[x]].push_back(ii(U[x],W[x])); edges.push_back(iii(W[x],ii(V[x],U[x]))); } rep(x,0,n){ vector<int> temp; for (auto &it:al[x]) temp.push_back(it.se); sort(all(temp)); if (sz(temp)>=3) extra[x]=temp[2]; } IDX=n; sort(all(edges)); for (auto &it:edges){ int u=find(it.se.fi),v=find(it.se.se); if (u==v){ loop.push_back(it); continue; } w[IDX]=it.fi; p[u]=p[v]=IDX; child[IDX].push_back(u); child[IDX].push_back(v); IDX++; } rep(node,IDX,0){ for (auto &it:child[node]){ d[it]=d[node]+1; int curr=pa[it][0]=node; rep(x,0,20){ if (curr==-1) break; curr=pa[it][x+1]=pa[curr][x]; } } } for (auto &it:loop){ int temp=lca(it.se.se,it.se.se); extra[temp]=min(extra[temp],it.fi); } rep(x,0,IDX){ for (auto &it:child[x]){ extra[x]=min(extra[x],extra[it]); } w[x]=max(w[x],extra[x]); } rep(x,IDX,0){ for (auto &it:child[x]){ w[it]=min(w[it],w[x]); } } } int getMinimumFuelCapacity(int u, int v) { if (w[IDX-1]>=INF) return -1; u=lca(u,v); if (w[u]!=INF) return w[u]; return w[pa[u][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...