Submission #349850

#TimeUsernameProblemLanguageResultExecution timeMemory
349850dooweyMousetrap (CEOI17_mousetrap)C++14
25 / 100
1119 ms112360 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; #define fi first #define se second #define mp make_pair #define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); const int N = (int)1e6 + 100; vector<int> T[N]; vector<pii> lis[N]; int dp[N]; int up[N]; int pp[N]; int trap; int mouse; void dfs(int u, int par){ int deg = 0; for(auto x : T[u]) if(x != par) deg ++ ; for(auto x : T[u]){ if(x==par) continue; pp[x]=u; up[x] += up[u] + (par != -1) * (deg - 1); dfs(x,u); lis[u].push_back(mp(dp[x],x)); } sort(lis[u].begin(), lis[u].end()); int mx = 0; if(lis[u].size() >= 2) mx = lis[u][lis[u].size() - 2].fi; dp[u]=mx+lis[u].size(); reverse(lis[u].begin(), lis[u].end()); } bool can(int mid){ int cur = mouse; int pv = -1; int can = 1; int cnt = 0; while(cur != trap){ cnt=0; for(int q = (int)lis[cur].size() - 1; q >= 0; q -- ){ if(lis[cur][q].se == pv) continue; cnt ++ ; } for(auto x : lis[cur]){ if(x.se == pv) continue; if(x.fi + up[cur] + cnt > mid){ if(can==0)return false; can--; } } pv=cur; cur=pp[cur]; can++; } return true; } int main(){ fastIO; int n; cin >> n; cin >> trap >> mouse; int u, v; for(int i = 1; i < n; i ++ ){ cin >> u >> v; T[u].push_back(v); T[v].push_back(u); } dfs(trap,-1); int lf = 0, rf = (int)n * 2; int mid; while(lf < rf){ mid = (lf + rf) / 2; if(can(mid)){ rf=mid; } else{ lf=mid+1; } } cout << lf << "\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...