Submission #702902

#TimeUsernameProblemLanguageResultExecution timeMemory
702902sdfsfMousetrap (CEOI17_mousetrap)C++11
20 / 100
3 ms1236 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int N = 2000; const long long INF = 1ll << 60; int F[N], n, t, m, ch[N], pr[N], bala[N], A[N], h[N], delta[N]; vector<int> adj[N]; vector<pair<int, int>> X[N]; void dfs2(int v, int p) { for (auto u: adj[v]) if (u != p) { bala[u] = bala[v] + ch[v] - 1; dfs2(u, v); } } void dfs(int v, int p) { pr[v] = p; vector<int> tmp; for (auto u: adj[v]) if (u != p) { h[u] = h[v] + 1; dfs(u, v); tmp.push_back(F[u]); ch[v]++; } sort(tmp.rbegin(), tmp.rend()); if (tmp.size() <= 1) F[v] = tmp.size(); else F[v] = tmp[1] + tmp.size(); } bool is_valid() { int s = 0; for (int i = 0; i <= n; i++) { s += A[i]; if (s > i + 1) { return false; } } return true; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> t >> m; t--, m--; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } if (m == t) { cout << 0 << '\n'; return 0; } dfs(t, t); ch[t] = 1; dfs2(t, t); int tmp_m = m; int child = -1; while (m != t) { for (auto u: adj[m]) if (u != pr[m] && u != child) { if (m == tmp_m) X[h[tmp_m] - h[u] + 1].emplace_back(F[u] + 1 + bala[u], u); else X[h[tmp_m] - h[u] + 1].emplace_back(F[u] + 1 + bala[u] - 1, u); } child = m; m = pr[m]; } for (int i = 0; i <= n; i++) sort(X[i].begin(), X[i].end()); int moves = 0; for (int i = 0; i <= n; i++) { int z = -1; for (int j = 0; j <= n; j++) if (X[j].size()) { if (z == -1 || X[j].back().first + delta[j] > X[z].back().first + delta[z]) z = j; } if (z == -1) { break; } int f = X[z].back().first + delta[z]; int u = X[z].back().second; X[z].pop_back(); for (int j = 0; j <= z; j++) delta[j]--; A[h[tmp_m] - h[u] + 1]++; if (!is_valid()) { cout << f + moves << '\n'; return 0; } moves++; } cout << moves << '\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...