Submission #716228

#TimeUsernameProblemLanguageResultExecution timeMemory
716228socpiteMousetrap (CEOI17_mousetrap)C++14
100 / 100
1010 ms182124 KiB
#include<bits/stdc++.h> using namespace std; #define f first #define s second typedef long double ld; typedef pair<int, pair<int, int>> ftype; const int maxn = 1e6+5; const int inf = 1e9+5; int n, m, t; int l, r, mid; vector<int> g[maxn]; int dp[maxn], onp[maxn]; vector<int> path; void onp_dfs(int x, int p){ if(x == t){ onp[x] = 1; return; } for(auto v: g[x]){ if(v == p)continue; onp_dfs(v, x); if(onp[v]){ dp[x]--; onp[x] = 1; } } if(onp[x] && x != t)path.push_back(x); //cout << x << " " << dp[x] << endl; } void dfs(int x, int p){ if(x == t)return; vector<int> val; for(auto v: g[x]){ if(v == p)continue; dfs(v, x); val.push_back(dp[v]); } if(val.empty())dp[x] = 0; else if(val.size() == 1)dp[x] = 1; else{ sort(val.begin(), val.end()); dp[x] = val[val.size()-2] + val.size(); } } int dfs2(int x, int p, int tot = 0){ if(x == t)return 0; for(auto v: g[x]){ if(v == p)continue; if(onp[v])tot+=dfs2(v, x, 0); } if(onp[x])tot+=g[x].size()-(p!=0)-1; dp[x] += tot; for(auto v: g[x]){ if(v == p)continue; if(!onp[v])dfs2(v, x, tot); } //cout << x << " " << dp[x] << " " << tot << endl; return tot; } bool chk(){ int used = 0, cap = 0; for(auto x: path){ cap++; int last = used; for(auto v: g[x]){ if(onp[v])continue; if(dp[v] + last > mid)used++; } //cout << used << endl; if(used > cap)return 0; } return used <= mid; } int main(){ ios::sync_with_stdio(false); cin.tie(0); //freopen("mousetrap.inp", "r", stdin); // freopen("mousetrap.out", "w", stdout); cin >> n >> t >> m; for(int i = 0; i < n-1; i++){ int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } onp_dfs(m, 0); reverse(path.begin(), path.end()); for(auto x: path){ for(auto v: g[x])if(!onp[v])dfs(v, x); } dfs2(m, 0); int l = 0, r = 2*maxn; while(l < r){ mid = (l+r)>>1; if(chk())r = mid; else l = mid+1; } cout << l; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...