Submission #1220514

#TimeUsernameProblemLanguageResultExecution timeMemory
1220514LucaIlieMousetrap (CEOI17_mousetrap)C++20
25 / 100
600 ms64172 KiB
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e6; const int MAX_N2 = 1e3; const int INF = 1e9; bool onPathTM[MAX_N + 1]; int parent[MAX_N + 1]; int dpArb[MAX_N + 1]; int dp[MAX_N2 + 1][MAX_N2 + 1]; vector<int> adj[MAX_N + 1]; void dfs(int u, int p) { parent[u] = p; for (int v: adj[u]) { if (v == p) continue; dfs(v, u); } } void calcDp(int u, int p) { int max1 = 0, max2 = 0; int sons = 0; for (int v: adj[u]) { if (v == p) continue; calcDp(v, u); if (!onPathTM[v]) { if (dpArb[v] > max1) { max2 = max1; max1 = dpArb[v]; } else if (dpArb[v] > max2) max2 = dpArb[v]; sons++; } } dpArb[u] = (sons > 0) + max2 + (sons > 1) + max(0, sons - 2); // printf("%d %d\n", u, dp[u]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, t, m; cin >> n >> t >> m; for (int e = 0; e < n - 1; e++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(t, 0); int w = m; vector<int> path; while (w != t) { onPathTM[w] = true; path.push_back(w); w = parent[w]; } onPathTM[w] = true; path.push_back(w); calcDp(t, 0); for (int i = path.size() - 1; i >= 0; i--) { int u = path[i]; vector<int> sons; for (int v: adj[u]) { if (onPathTM[v]) continue; sons.push_back(dpArb[v]); } sort(sons.begin(), sons.end()); reverse(sons.begin(), sons.end()); sons.push_back(0); for (int a = 1; a <= i + 1; a++) { dp[i][a] = INF; for (int d = 0; d <= a && d < (int)sons.size(); d++) { int cost = dp[i + 1][a - d + 1] + sons[d] + sons.size() - 1; dp[i][a] = min(dp[i][a], cost); } // printf("%d ", dp[i][a]); } // printf("\n"); } cout << dp[0][1] << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...