Submission #571717

#TimeUsernameProblemLanguageResultExecution timeMemory
571717BackNoobMousetrap (CEOI17_mousetrap)C++14
100 / 100
864 ms153420 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define endl '\n' #define MASK(i) (1LL << (i)) #define ull unsigned long long #define ld long double #define pb push_back #define all(x) (x).begin() , (x).end() #define BIT(x , i) ((x >> (i)) & 1) #define TASK "task" #define sz(s) (int) (s).size() using namespace std; const int mxN = 1e6 + 227; const int inf = 1e9 + 277; const int mod = 1e9 + 7; const ll infll = 1e18 + 7; const int base = 200; const int LOG = 20; template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } int n; int t; int m; int deg[mxN]; int dp[mxN]; int par[mxN]; bool spec[mxN]; vector<int> adj[mxN]; vector<int> path; void dfs(int u, int p) { for(int v : adj[u]) { if(v == p) continue; par[v] = u; dfs(v, u); } } void calcdp(int u, int p) { pair<int, int> best = {0, 0}; for(int v : adj[u]) { if(v == p) continue; deg[u]++; calcdp(v, u); int tmp = best.fi; if(maximize(best.fi, dp[v]) == true) best.se = tmp; else maximize(best.se, dp[v]); } dp[u] = best.se + deg[u]; } bool ok(int x) { int sumdeg = 0; for(auto it : path) sumdeg += deg[it]; if(sumdeg > x) return false; int cur = 0; int need = 0; for(auto u : path) { ++cur; int re = 0; for(int v : adj[u]) { if(spec[v] == true) continue; re += (dp[v] + sumdeg) <= x; need += (dp[v] + sumdeg) > x; } sumdeg -= re; if(need > cur) return false; } return true; } void solve() { cin >> n >> t >> m; for(int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } par[m] = 0; dfs(m, -1); int pos = t; while(pos != 0) { if(pos != t) path.pb(pos); pos = par[pos]; } reverse(all(path)); spec[t] = true; for(auto it : path) spec[it] = true; for(auto u : path) { for(int v : adj[u]) { if(spec[v] == false) { calcdp(v, u); deg[u]++; } } } // cout << dp[6] << ' ' << dp[7] << ' ' << dp[3] << endl; int lo = 0, hi = inf; while(lo + 1 < hi) { int mid = (lo + hi) / 2; if(ok(mid) == true) hi = mid; else lo = mid; } if(ok(lo)) cout << lo; else cout << hi; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen(TASK".in" , "r" , stdin); //freopen(TASK".out" , "w" , stdout); int tc = 1; // cin >> tc; while(tc--) { solve(); } 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...