Submission #310488

#TimeUsernameProblemLanguageResultExecution timeMemory
310488wiwihoSwapping Cities (APIO20_swap)C++14
100 / 100
681 ms75928 KiB
//#define NDEBUG #include "swap.h" #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define iceil(a, b) ((a + b - 1) / b) #define tomax(a, b) (a = max(a, b)) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} //#define TEST using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } vector<int> dsu, one, three; vector<vector<int>> anc, g; vector<int> ans, in, out; int findDSU(int a){ if(dsu[a] != a) dsu[a] = findDSU(dsu[a]); return dsu[a]; } void unionDSU(int a, int b){ a = findDSU(a); b = findDSU(b); if(a == b) return; dsu[b] = a; one[a] += one[b]; three[a] += three[b]; } int dfsts = 0; void dfs(int now, int p){ in[now] = dfsts++; if(ans[now] == -1) ans[now] = ans[p]; for(int i : g[now]) dfs(i, now); out[now] = dfsts++; } bool isAnc(int a, int b){ return in[a] <= in[b] && out[a] >= out[b]; } int lca(int a, int b){ if(isAnc(a, b)) return a; for(int i = 19; i >= 0; i--){ if(!isAnc(anc[a][i], b)) a = anc[a][i]; } return anc[a][0]; } void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) { vector<pair<int, pii>> e(M); for(int i = 0; i < M; i++){ e[i] = mp(W[i], mp(U[i], V[i])); } lsort(e); dsu.resize(N + M); for(int i = 0; i < N + M; i++) dsu[i] = i; one.resize(N + M); three.resize(N + M); int ts = N; anc.resize(N + M, vector<int>(20)); for(int i = 0; i < N + M; i++) anc[i][0] = i; ans.resize(N + M, -1); g.resize(N + M); in.resize(N + M); out.resize(N + M); vector<int> deg(N); for(auto i : e){ int w = i.F, u = i.S.F, v = i.S.S; if(deg[u] == 1) one[findDSU(u)]--; if(deg[v] == 1) one[findDSU(v)]--; deg[u]++; deg[v]++; if(deg[u] == 1) one[findDSU(u)]++; else if(deg[u] == 3) three[findDSU(u)]++; if(deg[v] == 1) one[findDSU(v)]++; else if(deg[v] == 3) three[findDSU(v)]++; u = findDSU(u); v = findDSU(v); int dv = ts++; if(u == v){ unionDSU(dv, u); anc[u][0] = dv; g[dv].eb(u); } else{ unionDSU(dv, u); unionDSU(dv, v); anc[u][0] = anc[v][0] = dv; g[dv].eb(u); g[dv].eb(v); } if(!one[dv] || three[dv]) ans[dv] = w; } // for(int i = 0; i < N + M; i++) cerr << anc[i][0] << " "; // cerr << "\n"; for(int i = 0; i < N + M; i++){ if(anc[i][0] == i) dfs(i, i); } for(int i = 1; i < 20; i++){ for(int j = 0; j < N + M; j++){ anc[j][i] = anc[anc[j][i - 1]][i - 1]; } } } int getMinimumFuelCapacity(int X, int Y) { if(findDSU(X) != findDSU(Y)) return -1; return ans[lca(X, Y)]; }
#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...