Submission #993818

#TimeUsernameProblemLanguageResultExecution timeMemory
993818AlbertsteinFactories (JOI14_factories)C++17
15 / 100
8018 ms202700 KiB
#include <bits/stdc++.h> #include "factories.h" #define pii pair<int, int> using namespace std; const long long maxn = 500001, mbit = 20, INF = LLONG_MAX / 3; int n; bool rem[maxn]; int sub_sz[maxn]; long long mini[maxn]; vector <pair<int, long long> > Dist[maxn]; vector <pii > G [maxn]; int dfs(int u, int p = -1){ sub_sz[u] = 1; for(auto [v, d] : G[u]){ if(v != p && !rem[v]){ sub_sz[u] += dfs(v, u); } } return sub_sz[u]; } void dfs2(int u, int p, long long dist, int r){ Dist[u].push_back({r, dist}); for(auto [v, d] : G[u]){ if(v != p && !rem[v]){ dfs2(v, u, dist + d, r); } } } int g_cen(int u, int sz, int p = -1){ for(auto [v, d] : G[u]){ if(v != p && !rem[v]){ if(sub_sz[v] * 2 > sz){ return g_cen(v, sz, u); } } } return u; } int sol(int r = 0){ int sz = dfs(r); int c = g_cen(r, sz); dfs2(c, -1, 0, c); rem[c] = 1; for(auto [v, d] : G[c]){ if(!rem[v]){ sol(v); } } return c; } void Init(int N, int A[], int B[], int D[]) { n = N; for(int i = 0; i < N - 1; i++){ int a = A[i], b = B[i], d = D[i]; G[a].push_back({b, d}); G[b].push_back({a, d}); } sol(); } long long Query(int S, int X[], int T, int Y[]) { long long ans = INF; fill(mini, mini + maxn, INF); for(int i = 0; i < S; i++){ int x = X[i]; for(auto [v, d] : Dist[x]){ mini[v] = min(mini[v], d); } } for(int i = 0; i < T; i++){ int y = Y[i]; for(auto [v, d] : Dist[y]){ ans = min(ans, mini[v] + d); } }//*/ return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...