Submission #161602

#TimeUsernameProblemLanguageResultExecution timeMemory
161602amoo_safarMousetrap (CEOI17_mousetrap)C++14
25 / 100
1054 ms94284 KiB
#include <bits/stdc++.h> #define pb push_back #define F first #define S second using namespace std; typedef long long ll; typedef string str; const ll Mod = 1e9 + 7; const int Maxn = 1e6 + 100; vector<ll> G[Maxn]; ll dp[Maxn], son[Maxn], m1[Maxn], m2[Maxn]; ll t, m; vector<ll> V; bool DFS(ll u, ll p){ bool res = (u == m), r2; ll cnt = 0; ll mx = 0; ll mx2 = 0; for(auto adj : G[u]){ if(adj == p) continue; r2 =DFS(adj, u); res |= r2; if(!r2){ cnt ++; if(dp[adj] > mx){ mx2 = mx; mx = dp[adj]; } else mx2 = max(mx2, dp[adj]); } } dp[u] = cnt + mx2; m2[u] = mx2; m1[u] = mx; son[u] = cnt; if(res){ V.pb(u); } return res; } ll dp2[Maxn]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n >> t >> m; ll u, v; for(int i = 1; i < n; i++){ cin >> u >> v; G[u].pb(v); G[v].pb(u); } DFS(t, -1); ll sz = V.size(); dp2[sz-1] = 0; ll sm = 0; for(int i = sz - 2; i >= 0; i--){ dp2[i] = min( max(dp2[i + 1], sm + m1[V[i]] + son[V[i]]), 1LL + max(dp2[i + 1], sm + m2[V[i]] + son[V[i]] - 1) ); sm += son[V[i]]; } cout << dp2[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...