Submission #122612

#TimeUsernameProblemLanguageResultExecution timeMemory
122612tinderFactories (JOI14_factories)C++14
100 / 100
7446 ms181652 KiB
#include "factories.h" #include <bits/stdc++.h> using namespace std; #define v first #define w second const int maxn = 5e5 + 5; using ll = long long; using ii = pair<ll, ll>; int N; vector<ii> g[maxn], t[maxn]; int in[maxn], tym; int lvl[maxn], dp[20][maxn]; ll dist[maxn]; ll f[maxn]; void dfs(int u, int p, ll dd) { if(p == -1) lvl[u] = 0; else lvl[u] = lvl[p] + 1; dist[u] = dd; dp[0][u] = p; for(int i = 1; i <= 18; i++) { int mid = dp[i - 1][u]; if(mid != -1) dp[i][u] = dp[i - 1][mid]; else break; } in[u] = tym++; for(ii e : g[u]) { if(e.v != p) { dfs(e.v, u, e.w + dd); } } } int __lca(int u, int v) { if(lvl[u] > lvl[v]) swap(u, v); int d = lvl[v] - lvl[u]; for(int i = 0; i < 19; i++) { if(d & (1 << i)) { v = dp[i][v]; } } if(u == v) return u; for(int i = 18; i >= 0; i--) { if(dp[i][u] != dp[i][v]) { u = dp[i][u]; v = dp[i][v]; } } return dp[0][u]; } void Init(int _N, int A[], int B[], int D[]) { N = _N; for(int i = 0; i < N - 1; i++) { int u = A[i], v = B[i], w = D[i]; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } memset(dp, -1, sizeof dp); dfs(0, -1, 0LL); } long long Query(int S, int X[], int T, int Y[]) { // find nodes vector<int> all; for(int i = 0; i < S; i++) { all.emplace_back(X[i]); } for(int i = 0; i < T; i++) { all.emplace_back(Y[i]); } sort(all.begin(), all.end(), [](int x, int y){ return in[x] < in[y]; }); all.erase(unique(all.begin(), all.end()), all.end()); int sz = all.size(); for(int i = 1; i < sz; i++) { all.emplace_back(__lca(all[i - 1], all[i])); } sort(all.begin(), all.end(), [](int x, int y){ return in[x] < in[y]; }); all.erase(unique(all.begin(), all.end()), all.end()); // clear for(int u : all) { t[u].clear(); f[u] = 1e18; } // build tree for(int i = 1; i < all.size(); i++) { int u = __lca(all[i - 1], all[i]); int v = all[i]; ll w = dist[v] - dist[u]; t[u].emplace_back(ii(v, w)); t[v].emplace_back(ii(u, w)); } // Dijkstra priority_queue<ii, vector<ii>, greater<ii>> Q; for(int i = 0; i < S; i++) { f[X[i]] = 0; Q.emplace(0, X[i]); } while(Q.size()) { int u = Q.top().second; Q.pop(); for(ii &e : t[u]) { if(f[e.v] > f[u] + e.w) { f[e.v] = f[u] + e.w; Q.emplace(f[e.v], e.v); } } } ll ans = 1e18; for(int i = 0; i < T; i++) { ans = min(ans, f[Y[i]]); } return ans; }

Compilation message (stderr)

factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:94:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < all.size(); i++) {
                 ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...