Submission #731612

#TimeUsernameProblemLanguageResultExecution timeMemory
731612stevancvSwapping Cities (APIO20_swap)C++14
0 / 100
4 ms7380 KiB
#include <bits/stdc++.h> #include "swap.h" #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 1e5 + 2; const int inf = 2e9 + 2; int link[3 * N], ok[3 * N], mx[3 * N], cnt[N], tsz; vector<int> g[3 * N]; int Find(int x) { if (x == link[x]) return x; return link[x] = Find(link[x]); } int par[3 * N][19], in[3 * N], out[3 * N], koji[3 * N]; void Dfs(int s, int e, int v) { if (ok[s] == 1) v = s; koji[s] = v; in[s] = ++tsz; par[s][0] = e; for (int i = 1; i < 19; i++) par[s][i] = par[par[s][i - 1]][i - 1]; for (int u : g[s]) { Dfs(u, s, v); } out[s] = tsz; } bool Ancestor(int u, int v) { return in[u] <= in[v] && out[v] <= out[u]; } int Lca(int u, int v) { if (Ancestor(u, v)) return u; if (Ancestor(v, u)) return v; for (int i = 18; i >= 0; i--) { if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i]; } return par[u][0]; } void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) { vector<array<int, 3>> e; for (int i = 0; i < m; i++) { u[i] += 1; v[i] += 1; e.push_back({w[i], u[i], v[i]}); } sort(e.begin(), e.end()); for (int i = 1; i <= n; i++) link[i] = i; tsz = n; for (auto u : e) { int c = u[0]; int a = u[1]; int b = u[2]; if (Find(a) != Find(b)) { cnt[a] += 1; cnt[b] += 1; tsz += 1; a = Find(a); b = Find(b); g[tsz].push_back(a); g[tsz].push_back(b); link[a] = link[b] = link[tsz] = tsz; if (ok[a] || ok[b] || cnt[a] >= 3 || cnt[b] >= 3) ok[tsz] = 1; mx[tsz] = c; continue; } cnt[a] += 1; cnt[b] += 1; tsz += 1; a = Find(a); g[tsz].push_back(a); link[a] = link[tsz] = tsz; ok[tsz] = 1; mx[tsz] = c; } tsz = 0; Dfs(n + m, 0, 0); } int getMinimumFuelCapacity(int x, int y) { x += 1; y += 1; int o = koji[Lca(x, y)]; return mx[o]; }
#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...