Submission #908195

#TimeUsernameProblemLanguageResultExecution timeMemory
908195typ_ikFactories (JOI14_factories)C++17
100 / 100
5644 ms369212 KiB
#include <bits/stdc++.h> #include "factories.h" #define ll long long #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define watch(x) cout << (#x) << " : " << x << '\n' #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const int N = 5e5 + 128; const ll INF = 1e15 + 128; vector < pair<int, int> > g[N]; int n, q; bool was[N]; int h[N]; ll res[N]; void dfs(int v, int p = -1) { h[v] = 1; for (auto& [to, len] : g[v]) if (to != p && !was[to]) dfs(to, v), h[v] += h[to]; } vector < pair<int, ll> > p[N]; void calc(int v, int cent, ll val, int pr = -1) { p[v].push_back(make_pair(cent, val)); for (auto& [to, len] : g[v]) if (to != pr && !was[to]) calc(to, cent, val+len, v); } int find_centroid(int v, int pr, int sz) { for (auto& [to, len] : g[v]) if (to != pr && h[to] > sz / 2 && !was[to]) return find_centroid(to, v, sz); return v; } void decompose(int v, int pr = -1) { dfs(v); int c = find_centroid(v, -1, h[v]); calc(c, c, 0); was[c] = true; for (auto& [to, len] : g[c]) if (!was[to]) decompose(to, c); } void update(int v) { for (auto& [x, d] : p[v]) res[x] = min(res[x], d); } void reset(int v) { for (auto& [x, d] : p[v]) res[x] = INF; } ll get(int v) { ll ans = INF; for (auto& [x, d] : p[v]) ans = min(ans, d + res[x]); return ans; } void Init(int N, int A[], int B[], int D[]) { n = N; for (int i = 0; i + 1 < n; i++) { int u = A[i], v = B[i], w = D[i]; g[u].push_back(make_pair(v, w)); g[v].push_back(make_pair(u, w)); } for (int i = 0; i <= n; i++) res[i] = INF; decompose(1); } long long Query(int S, int X[], int T, int Y[]) { for (int i = 0; i < S; i++) update(X[i]); ll tot = INF; for (int i = 0; i < T; i++) tot = min(tot, get(Y[i])); for (int i = 0; i < S; i++) reset(X[i]); return tot; } // main() { // boost; // int A[] = {0, 1, 2, 2, 4, 1}; // int B[] = {1, 2, 3, 4, 5, 6}; // int D[] = {4, 4, 5, 6, 5, 3}; // Init(7, A, B, D); // int X[] = {0, 6}; // int Y[] = {3, 4}; // cout << Query(2, X, 2, Y) << '\n'; // return 0; // }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...