///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 1e6 + 10;
ll n, t, m;
ll dp[MXN], dad[MXN];
ll limb[MXN], deg[MXN];
vector<ll> DpV[MXN], adj[MXN];
bool inp[MXN]; //in path
void prep(ll u){
inp[u] = (m == u);
deg[u] = (u == t ? 1 : adj[u].size() - 1);
for(auto v : adj[u]){
if(v == dad[u]) continue;
dad[v] = u, prep(v);
inp[u] |= inp[v];
}
}
void dfs(ll u){
if(dad[u] == t && !inp[u]) return;
for(auto v : adj[u]){
if(v == dad[u]) continue;
limb[v] = limb[u] + deg[u] - (inp[u] && u != m);
dfs(v);
if(!inp[v]) DpV[u].push_back(-dp[v]);
}
sort(DpV[u].begin(), DpV[u].end());
if(deg[u] >= 2) dp[u] = -DpV[u][1];
else dp[u] = limb[u] + (deg[u] > 0);
}
bool check(ll k){
ll u = m, c = 1; //streak
while(u != t){
ll my = lower_bound(DpV[u].begin(), DpV[u].end(), -k) - DpV[u].begin();
if(c < my || k < my) return 0;
k -= my, c -= my, u = dad[u], c ++;
}
return 1;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n >> t >> m;
if(m == t) return cout << 0, 0;
for(int i = 1; i < n; i ++){
ll u, v; cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
prep(t), dfs(t);
ll low = -1, hig = n + 1;
while(hig - low > 1){
ll mid = (low + hig) / 2;
if(check(mid)) hig = mid;
else low = mid;
}
cout << hig << '\n';
return 0;
}
/*!
HE'S AN INSTIGATOR,
ENEMY ELIMINATOR,
AND WHEN HE KNOCKS YOU BETTER
YOU BETTER LET HIM IN.
*/
//! N.N
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47308 KB |
Output is correct |
2 |
Correct |
27 ms |
47212 KB |
Output is correct |
3 |
Correct |
27 ms |
47248 KB |
Output is correct |
4 |
Correct |
27 ms |
47396 KB |
Output is correct |
5 |
Correct |
28 ms |
47332 KB |
Output is correct |
6 |
Correct |
29 ms |
47280 KB |
Output is correct |
7 |
Correct |
27 ms |
47300 KB |
Output is correct |
8 |
Correct |
27 ms |
47212 KB |
Output is correct |
9 |
Correct |
28 ms |
47428 KB |
Output is correct |
10 |
Correct |
26 ms |
47232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
486 ms |
134424 KB |
Output is correct |
2 |
Correct |
422 ms |
125672 KB |
Output is correct |
3 |
Correct |
1278 ms |
135720 KB |
Output is correct |
4 |
Correct |
592 ms |
91620 KB |
Output is correct |
5 |
Correct |
1223 ms |
135724 KB |
Output is correct |
6 |
Correct |
1237 ms |
135784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47308 KB |
Output is correct |
2 |
Correct |
27 ms |
47212 KB |
Output is correct |
3 |
Correct |
27 ms |
47248 KB |
Output is correct |
4 |
Correct |
27 ms |
47396 KB |
Output is correct |
5 |
Correct |
28 ms |
47332 KB |
Output is correct |
6 |
Correct |
29 ms |
47280 KB |
Output is correct |
7 |
Correct |
27 ms |
47300 KB |
Output is correct |
8 |
Correct |
27 ms |
47212 KB |
Output is correct |
9 |
Correct |
28 ms |
47428 KB |
Output is correct |
10 |
Correct |
26 ms |
47232 KB |
Output is correct |
11 |
Correct |
28 ms |
47308 KB |
Output is correct |
12 |
Correct |
28 ms |
47324 KB |
Output is correct |
13 |
Correct |
27 ms |
47372 KB |
Output is correct |
14 |
Correct |
29 ms |
47436 KB |
Output is correct |
15 |
Correct |
29 ms |
47376 KB |
Output is correct |
16 |
Correct |
27 ms |
47316 KB |
Output is correct |
17 |
Correct |
28 ms |
47420 KB |
Output is correct |
18 |
Correct |
28 ms |
47308 KB |
Output is correct |
19 |
Correct |
28 ms |
47300 KB |
Output is correct |
20 |
Correct |
29 ms |
47360 KB |
Output is correct |
21 |
Correct |
28 ms |
47328 KB |
Output is correct |
22 |
Correct |
28 ms |
47304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47308 KB |
Output is correct |
2 |
Correct |
27 ms |
47212 KB |
Output is correct |
3 |
Correct |
27 ms |
47248 KB |
Output is correct |
4 |
Correct |
27 ms |
47396 KB |
Output is correct |
5 |
Correct |
28 ms |
47332 KB |
Output is correct |
6 |
Correct |
29 ms |
47280 KB |
Output is correct |
7 |
Correct |
27 ms |
47300 KB |
Output is correct |
8 |
Correct |
27 ms |
47212 KB |
Output is correct |
9 |
Correct |
28 ms |
47428 KB |
Output is correct |
10 |
Correct |
26 ms |
47232 KB |
Output is correct |
11 |
Correct |
486 ms |
134424 KB |
Output is correct |
12 |
Correct |
422 ms |
125672 KB |
Output is correct |
13 |
Correct |
1278 ms |
135720 KB |
Output is correct |
14 |
Correct |
592 ms |
91620 KB |
Output is correct |
15 |
Correct |
1223 ms |
135724 KB |
Output is correct |
16 |
Correct |
1237 ms |
135784 KB |
Output is correct |
17 |
Correct |
28 ms |
47308 KB |
Output is correct |
18 |
Correct |
28 ms |
47324 KB |
Output is correct |
19 |
Correct |
27 ms |
47372 KB |
Output is correct |
20 |
Correct |
29 ms |
47436 KB |
Output is correct |
21 |
Correct |
29 ms |
47376 KB |
Output is correct |
22 |
Correct |
27 ms |
47316 KB |
Output is correct |
23 |
Correct |
28 ms |
47420 KB |
Output is correct |
24 |
Correct |
28 ms |
47308 KB |
Output is correct |
25 |
Correct |
28 ms |
47300 KB |
Output is correct |
26 |
Correct |
29 ms |
47360 KB |
Output is correct |
27 |
Correct |
28 ms |
47328 KB |
Output is correct |
28 |
Correct |
28 ms |
47304 KB |
Output is correct |
29 |
Correct |
27 ms |
47308 KB |
Output is correct |
30 |
Correct |
461 ms |
147724 KB |
Output is correct |
31 |
Correct |
447 ms |
147764 KB |
Output is correct |
32 |
Correct |
517 ms |
218288 KB |
Output is correct |
33 |
Correct |
503 ms |
249536 KB |
Output is correct |
34 |
Correct |
1292 ms |
148920 KB |
Output is correct |
35 |
Correct |
1283 ms |
148880 KB |
Output is correct |
36 |
Correct |
1267 ms |
157104 KB |
Output is correct |
37 |
Correct |
1317 ms |
157088 KB |
Output is correct |
38 |
Correct |
1020 ms |
156088 KB |
Output is correct |
39 |
Correct |
1048 ms |
156144 KB |
Output is correct |
40 |
Correct |
1102 ms |
156120 KB |
Output is correct |