Submission #59228

#TimeUsernameProblemLanguageResultExecution timeMemory
59228gusfringFactories (JOI14_factories)C++14
33 / 100
8083 ms518552 KiB
#include "factories.h" #include "bits/stdc++.h" using namespace std; typedef pair <int, int> pii; const int logn = 25; int sub[500010]; long long dp[logn][500010]; int deg[500010]; long long dis[500010]; vector <pii> g[500010]; bool vis[500010]; int anc[500010]; long long aux[500010]; const long long inf = 1e16; void subtree(int x, int par) { sub[x] = 1; for(auto i : g[x]) { if(i.first != par && vis[i.first] == false) { subtree(i.first, x); sub[x] += sub[i.first]; } } } int centroid(int x, int par, int range) { for(auto i : g[x]) { if(i.first != par && vis[i.first] == false) { if(sub[i.first] > range) return centroid(i.first, x, range); } } return x; } void dfs(int x, int par, int level) { assert(level < logn); dp[level][x] = dis[x]; for(auto i : g[x]) { if(i.first != par && vis[i.first] == false) { dis[i.first] = i.second + dis[x]; dfs(i.first, x, level); } } } void create(int x, int par, int level) { subtree(x, -1); int c = centroid(x, -1, sub[x] >> 1); dis[c] = 0; dfs(c, -1, level); deg[c] = level; vis[c] = true; anc[c] = par; for(auto i : g[c]) { if(vis[i.first] == false) { create(i.first, c, level + 1); } } } void put(int x) { int cur = x; int lev = deg[x]; while(cur >= 0) { aux[cur] = min(aux[cur], dp[lev][x]); cur = anc[cur]; --lev; } } void remove(int x) { int cur = x; while(cur >= 0) { aux[cur] = inf; cur = anc[cur]; } } long long get(int x) { int cur = x; long long ans = inf; int lev = deg[x]; while(cur >= 0) { ans = min(ans, dp[lev][x] + aux[cur]); cur = anc[cur]; --lev; } return ans; } void Init(int N, int A[], int B[], int D[]) { for(int i = 0; i < N-1; i++) { g[A[i]].push_back(pii(B[i], D[i])); g[B[i]].push_back(pii(A[i], D[i])); } create(0, -1, 0); for(int i = 0; i < N; i++) { aux[i] = inf; } } long long Query(int S, int X[], int T, int Y[]) { for(int i = 0; i < S; i++) { put(X[i]); } long long ans = inf; for(int i = 0; i < T; i++) { ans = min(ans, get(Y[i])); } for(int i = 0; i < S; i++) { remove(X[i]); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...