Submission #702890

#TimeUsernameProblemLanguageResultExecution timeMemory
702890sdfsfMousetrap (CEOI17_mousetrap)C++11
25 / 100
918 ms113832 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int N = 1e6 + 50; const long long INF = 1ll << 60; int F[N], n, t, m, ch[N], pr[N], bala[N], A[N], h[N]; vector<int> adj[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); vector<pair<int, int>> X; int tmp_m = m; int child = -1; int co = 0; while (m != t) { for (auto u: adj[m]) if (u != pr[m] && u != child) { if (m == tmp_m) X.emplace_back(F[u] + 1 + bala[u], u); else X.emplace_back(F[u] + 1 + bala[u] - 1, u); } child = m; m = pr[m]; assert(co++ <= n); } sort(X.rbegin(), X.rend()); for (auto p: X) { int f = p.first; int u = p.second; assert(h[tmp_m] - h[u] + 1 >= 0); A[h[tmp_m] - h[u] + 1]++; if (!is_valid()) { cout << f << '\n'; return 0; } } cout << 0 << '\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...