Submission #62130

#TimeUsernameProblemLanguageResultExecution timeMemory
62130FutymyCloneFactories (JOI14_factories)C++14
0 / 100
159 ms16508 KiB
#include <bits/stdc++.h> #include "factories.h" using namespace std; const int N = 5e5 + 5; vector <pair <int, int> > g[N]; int h[N], child[N], par[N]; bool visited[N]; long long dist[20][N], ans[N]; void dfs (int u, int p) { child[u] = 1; for (auto v: g[u]) { if (v.first != p && !visited[v.first]) { dfs(v.first, u); child[u] += child[v.first]; } } } int findcen (int u, int p, int sz) { for (auto v: g[u]) { if (v.first != p && !visited[v.first] && child[v.first] >= sz / 2) return findcen(v.first, u, sz); } return u; } void redfs (int level, int u, int p, int cost) { if (p != -1) dist[level][u] = dist[level][p] + cost; for (auto v: g[u]) { if (v.first != p && !visited[v.first]) { redfs(level, v.first, u, v.second); } } } void centroid (int root, int p) { dfs(root, -1); int cen = findcen(root, -1, child[root]); visited[cen] = true; par[cen] = p; if (p != -1) h[cen] = h[p] + 1; redfs(h[cen], cen, -1, 0); for (auto v: g[cen]) if (v.first != p && !visited[v.first]) centroid(v.first, cen); } void update (int u) { for (int v = u; v != -1; v = par[v]) ans[v] = min(ans[v], dist[h[v]][u]); } long long query (int u) { long long res = 1e18; for (int v = u; v != -1; v = par[v]) res = min(res, ans[v] + dist[h[v]][u]); } void Init (int N, int A[], int B[], int D[]) { for (int i = 0; i < N - 1; i++) { g[A[i]].push_back(make_pair(B[i], D[i])); g[B[i]].push_back(make_pair(A[i], D[i])); } memset(ans, 0x3f, sizeof(ans)); centroid(0, -1); } long long Query (int S, int X[], int T, int Y[]) { if (S < T) { for (int i = 0; i < S; i++) update(X[i]); long long res = 1e18; for (int i = 0; i < T; i++) res = min(res, query(Y[i])); memset(ans, 0x3f, sizeof(ans)); return res; } else { for (int i = 0; i < T; i++) update(Y[i]); long long res = 1e18; for (int i = 0; i < S; i++) res = min(res, query(X[i])); memset(ans, 0x3f, sizeof(ans)); return res; } }

Compilation message (stderr)

factories.cpp: In function 'long long int query(int)':
factories.cpp:58:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...