Submission #1066043

#TimeUsernameProblemLanguageResultExecution timeMemory
1066043Gromp15Closing Time (IOI23_closing)C++17
83 / 100
1051 ms566168 KiB
#include <bits/stdc++.h> #include "closing.h" #define ar array #define ll long long #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using namespace std; template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const ll INF = 1e18; int max_score(int n, int X, int Y, long long K, std::vector<int> U, std::vector<int> V, std::vector<int> W) { vector<vector<ar<int, 2>>> adj(n+1); for (int i = 0; i < n-1; i++) { adj[U[i]].push_back({V[i], W[i]}); adj[V[i]].push_back({U[i], W[i]}); } vector<vector<ll>> d(2, vector<ll>(n, INF)); vector<int> p(n, -1); auto dfs = [&](auto&& s, int v, vector<ll>& dist) -> void { for (auto [u, w] : adj[v]) if (u != p[v]) { p[u] = v, dist[u] = dist[v] + w; s(s, u, dist); } }; d[0][X] = 0, d[1][Y] = 0; dfs(dfs, X, d[0]); fill(all(p), -1); dfs(dfs, Y, d[1]); if (d[0][Y] > 2*K) { vector<ll> D(2*n); for (int i = 0; i < n; i++) D[i] = d[0][i], D[i+n] = d[1][i]; sort(all(D)); int ans = 0; for (auto x : D) { if (K - x >= 0) { K -= x, ans++; } else break; } return ans; } vector<vector<ar<ll, 4>>> dp(n, vector<ar<ll, 4>>(2*n+1, {INF, INF, INF, INF})); vector<int> tin(n), tout(n), sub(n); int timer = 0; auto dfs3 = [&](auto&& s, int v, int px) -> void { tin[v] = timer++, sub[v] = 1; for (auto [u, w] : adj[v]) if (u != px) { s(s, u, v); sub[v] += sub[u]; } tout[v] = timer-1; }; dfs3(dfs3, X, -1); auto anc = [&](int x, int y) { return tin[x] <= tin[y] && tout[y] <= tout[x]; }; auto dfs2 = [&](auto&& s, int v, int px) -> void { dp[v][0][0] = 0; dp[v][1][1] = d[0][v]; dp[v][1][2] = d[1][v]; dp[v][2][3] = max(d[0][v], d[1][v]); int tot = 2; sort(all(adj[v]), [&](auto x, auto y) { return anc(x[0], Y) != anc(y[0], Y) ? anc(x[0], Y) : x[0] < y[0]; }); for (auto [u, w] : adj[v]) if (u != px) { s(s, u, v); assert(tot <= 2*n); for (int i = tot; i >= 0; i--) { for (int j = 2*sub[u]; j >= 0; j--) { assert(i+j<=2*n); ckmin(dp[v][i+j][0], dp[v][i][0] + dp[u][j][0]); if (anc(u, Y)) ckmin(dp[v][i+j][0], dp[v][i][0] + dp[u][j][2]); ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][0]); ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][1]); if (anc(u, Y)) { ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][2]); ckmin(dp[v][i+j][1], dp[v][i][1] + dp[u][j][3]); } if (anc(u, Y)) { if (i == 1) dp[v][i+j][2] = dp[v][i][2] + dp[u][j][2]; } else { ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][0]); ckmin(dp[v][i+j][2], dp[v][i][2] + dp[u][j][2]); } if (anc(u, Y)) { if (i == 2) dp[v][i+j][3] = dp[v][i][3] + min(dp[u][j][2], dp[u][j][3]); } else { ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][0]); ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][1]); ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][2]); ckmin(dp[v][i+j][3], dp[v][i][3] + dp[u][j][3]); } } } tot += 2*sub[u]; } }; dfs2(dfs2, X, -1); int ans = 0; for (int j = 1; j <= 2*n; j++) for (int k = 0; k < 4; k++) if (dp[X][j][k] <= K) ckmax(ans, j); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...