제출 #262203

#제출 시각아이디문제언어결과실행 시간메모리
262203Falcon공장들 (JOI14_factories)C++14
15 / 100
8034 ms159348 KiB
#pragma GCC optimize("O2") #include <bits/stdc++.h> #include "factories.h" #ifdef DEBUG #include "debug.hpp" #endif using namespace std; #define all(c) (c).begin(), (c).end() #define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); it++) #define rep(i, N) for(int i = 0; i < (N); i++) #define rep1(i, N) for(int i = 1; i <= (N); i++) #define rep2(i, s, e) for(int i = (s); i <= (e); i++) #define rep3(i, s, e, d) for(int i = (s); (d) >= 0 ? i <= (e) : i >= (e); i += (d)) #define pb push_back #ifdef DEBUG #define debug(x...) {dbg::depth++; string dbg_vals = dbg::to_string(x); dbg::depth--; dbg::fprint(__func__, __LINE__, #x, dbg_vals);} #define light_debug(x) {dbg::light = 1; dbg::dout << __func__ << ":" << __LINE__ << " " << #x << " = " << x << endl; dbg::light = 0;} #else #define debug(x...) #define light_debug(x) #endif template<typename T> T& ckmin(T& a, T b){ return a = a > b ? b : a; } template<typename T> T& ckmax(T& a, T b){ return a = a < b ? b : a; } using ll = long long; using pii = pair<int, int>; using vi = vector<int>; class CentroidDecomposition{ vector<set<int>> adj; vi size, par; int get_size(int v, int p){ size[v] = 1; for(auto u : adj[v]) if(u != p) size[v] += get_size(u, v); return size[v]; } int get_centroid(int v, int p, int sz){ for(auto u : adj[v]) if(u != p && size[u] > sz / 2) return get_centroid(u, v, sz); return v; } void build(int v, int p){ get_size(v, p); int c = get_centroid(v, p, size[v]); par[c] = p; for(auto u : adj[c]) adj[u].erase(c), build(u, c); adj[c].clear(); } public: void operator()(vector<vector<pii>>& adj_){ int n = adj_.size(); adj.resize(n); rep(i, n) for(auto e : adj_[i]) adj[i].insert(e.first); size.resize(n), par.resize(n); build(0, -1); } int operator [](int i) const { return par[i]; } }; class TreeDist{ int n; vi dep; vector<ll> dist; vector<vi> par; void dfs(int v, int depth, ll dis, int p, vector<vector<pii>>& adj){ dep[v] = depth; dist[v] = dis; par[v][0] = p; for(auto u : adj[v]) if(u.first != p) dfs(u.first, depth + 1, dis + u.second, v, adj); } public: void operator()(vector<vector<pii>>& adj){ n = adj.size(); dep.resize(n), dist.resize(n); par = vector<vi>(n, vi(__lg(n) + 1)); dfs(0, 0, 0, 0, adj); rep1(k, __lg(n)) rep(i, n) par[i][k] = par[par[i][k - 1]][k - 1]; } int lca(int u, int v) const { if(dep[u] < dep[v]) swap(u, v); for(int k = dep[u] - dep[v]; k; k -= 1 << __lg(k)) u = par[u][__lg(k)]; if(u == v) return u; for(int k = __lg(n); k >= 0; k--) if(par[u][k] != par[v][k]) u = par[u][k], v = par[v][k]; assert(par[u][0] == par[v][0]); return par[u][0]; } inline ll distance(int u, int v) const { return dist[u] + dist[v] - 2 * dist[lca(u, v)]; } }; const ll INF = 1LL << 60; int N; vector<vector<pii>> adj; CentroidDecomposition decomp; TreeDist paths; vector<ll> nearest; vi upd; void Init(int _N, int A[], int B[], int D[]) { #ifdef DEBUG freopen("debug", "w", stderr); #endif N = _N; adj.resize(N), nearest.resize(N); fill(all(nearest), INF); rep(i, N - 1) adj[A[i]].pb({B[i], D[i]}), adj[B[i]].pb({A[i], D[i]}); decomp(adj); paths(adj); } long long Query(int S, int X[], int T, int Y[]) { auto update = [&](int v){ for(int v_ = v; ~v; v = decomp[v]) ckmin(nearest[v], paths.distance(v_, v)), upd.pb(v); }; auto query = [&](int v){ ll a = INF; for(int v_ = v; ~v; v = decomp[v]) ckmin(a, nearest[v] + paths.distance(v_, v)); return a; }; ll ans = INF; rep(i, S) update(X[i]); rep(i, T) ckmin(ans, query(Y[i])); for(int i : upd) nearest[i] = INF; upd.clear(); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...