Submission #282326

#TimeUsernameProblemLanguageResultExecution timeMemory
282326shrek12357Mousetrap (CEOI17_mousetrap)C++14
100 / 100
1142 ms248824 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; vector<ll> S[Maxn]; 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){ S[u].pb(adj); 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]; vector< pair<ll, ll> > A; ll C[Maxn]; bool CMP(ll i, ll j){ return dp[i] > dp[j]; } ll ps[Maxn]; bool check(ll x){ ll M = V.size(); ll c = 0; for(int i = 0; i < M - 1; i++){ ll SS = S[V[i]].size(); for(int j = 0; j < SS; j++){ if(c + dp[S[V[i]][j]] + (SS - j) + ps[i] > x) { c++; } else break; } //cerr << c << '\n'; if(c > i + 1) return false; } //cerr << "!!!" << x << '\n'; return (c <= x); } 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; ps[sz-1] = 0; for(int i = sz - 2; i >= 0; i--){ ps[i] = sm; sm += son[V[i]]; //cerr << son[V[i]] << '\n'; ll K = 0; sort(S[V[i]].begin(), S[V[i]].end(), CMP); for(auto ch : S[V[i]]){ //cerr << '!' << ch << ' ' << dp[ch] << ' ' << sm << '\n'; A.pb({dp[ch] + sm - K, i + 1}); K ++; } dp2[i] = min( max(dp2[i + 1], sm-son[V[i]] + m1[V[i]] + son[V[i]]), 1LL + max(dp2[i + 1], sm-son[V[i]] + m2[V[i]] + son[V[i]] - 1) ); } sort(A.begin(), A.end()); reverse(A.begin(), A.end()); ll M = A.size(); ll cnt = 1; //cerr << '\n'; //dp2[0] = 100000000; /* ll ans = min(dp2[0], (M > 0 ? A[0].F : 0)); bool fl=true;; A.pb({0, 0}); for(int i = 0; i < M; i++){ for(int j = A[i].S; j <= n; j++){ C[j] ++; if(C[j] > j) fl = false; } //cerr << i << " " << fl << '\n'; if(fl) ans = min(ans, i + 1 + A[i + 1].F); cerr << i + 1 + A[i + 1].F << '\n'; } cout << ans << '\n'; */ //cout << dp2[0] << '\n'; ll l = -1, r = n, mid; while(l + 1 < r){ mid = (l + r) >> 1; if(check(mid)) r = mid; else l = mid; } //cerr << check(1) << '\n'; cout << r; return 0; }

Compilation message (stderr)

mousetrap.cpp: In function 'int main()':
mousetrap.cpp:109:5: warning: unused variable 'M' [-Wunused-variable]
  109 |  ll M = A.size();
      |     ^
mousetrap.cpp:110:5: warning: unused variable 'cnt' [-Wunused-variable]
  110 |  ll cnt = 1;
      |     ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...