#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 300005;
int n, a, b;
vector<int> g[mxN];
int par[mxN];
void dfs1(int v, int p){
par[v] = p;
for(auto u : g[v]){
if(u != p)dfs1(u, v);
}
}
bool bio[mxN];
int dfs2(int v, int p){
vector<int> vec;
for(auto u : g[v]){
if(u != p){
int X = dfs2(u, v);
vec.pb(X);
}
}
int kol = 0; sort(rall(vec));
ff(i,0,sz(vec) - 1)kol = max(kol, i + 1 + vec[i]);
return kol;
}
int m = 0;
vector<int> kol[mxN];
bool pref[mxN];
bool sufi[mxN];
bool check(int X){
ff(i,1,m)pref[i] = sufi[i] = 0;
int uk = 0; pref[0] = 1;
ff(i,1,m){
int len = sz(kol[i]); int ide = len + 1, mn = 0;
ff(j,1,len){
int z = X - kol[i][j - 1] - uk;
while(z < ide){
mn = ide;
ide -= 1;
}
ide -= 1;
}
if(ide > 0)mn = 1;
if(ide < 0){
// los
break;
}
pref[i] = 1; uk += mn;
}
uk = 0; sufi[m + 1] = 1;
fb(i,m,1){
int len = sz(kol[i]); int ide = len + 1, mn = 0;
ff(j,1,len){
int z = X - kol[i][j - 1] - uk;
while(z < ide){
mn = ide;
ide -= 1;
}
ide -= 1;
}
if(ide > 0)mn = 1;
if(ide < 0){
// los
break;
}
sufi[i] = 1; uk += mn;
}
ff(i,0,m)if(pref[i] && sufi[i + 1])return 1;
return 0;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> a >> b;
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs1(a, -1);
vector<int> put;
for(int i = b; i != -1; i = par[i]){
put.pb(i); bio[i] = 1;
}
m = sz(put);
ff(i,0,m - 1){
for(auto f : g[put[i]]){
if(!bio[f]){
int X = dfs2(f, put[i]);
kol[i + 1].pb(X);
}
}
sort(all(kol[i + 1]));
}
int l = 1, r = n, ans = 0;
while(l <= r){
int mid = (l + r) / 2;
if(check(mid)){
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
cout << ans << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
7 ms |
14432 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
31612 KB |
Output is correct |
2 |
Correct |
91 ms |
32764 KB |
Output is correct |
3 |
Correct |
90 ms |
32364 KB |
Output is correct |
4 |
Correct |
89 ms |
33708 KB |
Output is correct |
5 |
Correct |
90 ms |
31092 KB |
Output is correct |
6 |
Correct |
86 ms |
31056 KB |
Output is correct |
7 |
Correct |
94 ms |
32624 KB |
Output is correct |