Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///services.msc / информация... / автоматически
- #include<bits/stdc++.h>
- #define pb push_back
- #define ll long long
- #define ld long double
- #define F first
- #define S second
- using namespace std;
- mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
- const ll M = 1e9 + 7;
- const ll N = 2e6 + 7;
- int n, t, m, x, y, f[N], d[N], p[N];
- vector<int> g[N], a, F[N];
- bool fl[N];
- void bfs(int v){
- queue<int> q;
- q.push(v);
- p[v] = v;
- while (!q.empty()){
- int u = q.front();
- q.pop();
- for (auto to : g[u])
- if (!p[to]){
- p[to] = u;
- q.push(to);
- }
- }
- return;
- }
- void dfs(int v, int p){
- if (g[v].size() == 1){
- f[v] = 0;
- return;
- }
- vector<int> all;
- for (auto to : g[v]){
- if (to == p) continue;
- dfs(to, v);
- all.pb(f[to]);
- }
- sort(all.rbegin(), all.rend());
- if (all.size() == 1) f[v] = 1;
- else f[v] = all[1] + all.size();
- return;
- }
- bool can(int x){
- int cur = 1, y = x;
- for (int i = a.size() - 1; i >= 0; i--){
- int p = 0;
- while (p < F[i].size() && F[i][p] > y) p++;
- cur -= p;
- if (cur < 0) return 0;
- cur++;
- y -= p;
- if (y < 0) return 0;
- }
- return 1;
- }
- int32_t main(){
- ios_base::sync_with_stdio(0);
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- cin >> n >> t >> m;
- for (int i = 1; i < n; i++){
- cin >> x >> y;
- g[x].pb(y);
- g[y].pb(x);
- }
- bfs(t);
- int v = m;
- fl[v] = 1;
- while (v != t){
- a.pb(v);
- v = p[v];
- fl[v] = 1;
- }
- reverse(a.begin(), a.end());
- for (int i = 0; i < a.size(); i++){
- d[i] = (i ? d[i - 1] : 0);
- for (auto to : g[a[i]])
- if (!fl[to]) dfs(to, a[i]), d[i]++;
- for (auto to : g[a[i]])
- if (!fl[to]){
- f[to] += d[i];
- F[i].pb(f[to]);
- }
- sort(F[i].rbegin(), F[i].rend());
- }
- int l = 0, r = 1e9, ans = 1e9;
- while (l <= r){
- int mid = (l + r)/2;
- if (can(mid)){
- ans = min(ans, mid);
- r = mid - 1;
- }
- else l = mid + 1;
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement