Submission #585121

#TimeUsernameProblemLanguageResultExecution timeMemory
585121amunduzbaevMousetrap (CEOI17_mousetrap)C++17
0 / 100
310 ms66800 KiB
#include "bits/stdc++.h" using namespace std; #define ar array typedef int64_t ll; //~ #define int ll const int N = 1e6 + 5; vector<int> edges[N]; int n, t, m, dp[N], is[N]; int c[N]; void dfs(int u, int p = -1){ is[u] = (u == t), c[u] = -1; vector<int> t; for(auto x : edges[u]){ if(x == p) continue; dfs(x, u); is[u] |= is[x]; if(!is[x]) t.push_back(x); else c[u] = x; } sort(t.begin(), t.end(), [&](int& i, int& j){ return dp[i] > dp[j]; }); edges[u] = t; if(t.empty()) dp[u] = 0; else { dp[u] = t.size(); if((int)t.size() > 1) dp[u] += dp[t[1]]; } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n>>t>>m; if(t == m){ cout<<0<<"\n"; return 0; } for(int i=1;i<n;i++){ int a, b; cin>>a>>b; edges[a].push_back(b); edges[b].push_back(a); } dfs(m); int tot = 0, u = m; vector<int> v; while(u != t){ v.push_back(edges[u].size()); tot += edges[u].size(); u = c[u]; } for(int i=(int)v.size() - 2;~i;i--){ v[i] += v[i+1]; } auto check = [&](int mx){ int u = m, pos = 0, prev = 0, in = 0; while(u != t){ pos++; for(auto x : edges[u]){ if(dp[x] + prev + v[in] > mx) pos--, prev++; } if(pos < 0) return false; u = c[u]; } return true; }; int l = tot, r = 1e9; while(l < r){ int m = (l + r) >> 1; if(check(m)) r = m; else l = m + 1; } //~ while(m != t){ //~ pos++; //~ vector<int>& t = edges[m]; //~ sort(t.begin(), t.end(), [&](int& i, int& j){ //~ return dp[i] > dp[j]; //~ }); //~ if(pos >= (int)t.size()){ //~ pos -= (int)t.size(); //~ res += (int)t.size(); //~ } else { //~ res += dp[t[pos]] + (int)t.size(); //~ pos = 1e9; //~ } //~ m = c[m]; //~ } cout<<l<<"\n"; } /* 8 7 1 1 2 2 3 3 4 3 5 3 6 4 7 4 8 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...