#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
const int maxn = 1e6 + 5;
const int INF = 1e18;
vector<int> adj[maxn];
int dp[maxn], par[maxn];
int n, t, m;
void dfs(int pos, int prev){
if(prev != -1) adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), prev));
int mx = 0, sc = 0;
for(auto x : adj[pos]){
par[x] = pos;
dfs(x, pos);
if(dp[x] >= mx) sc = mx, mx = dp[x];
else sc = max(sc, dp[x]);
}
dp[pos] = sc + adj[pos].size();
}
signed main(void){
fastio;
cin>>n>>t>>m;
t--, m--;
for(int i = 0; i < n - 1; i++){
int a, b;
cin>>a>>b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(t, -1);
int ttld = 0;
int l = -1, r = n;
while(l + 1 < r){
int mid = (l + r) / 2; // cost to lead mouse into a dead end
int ok = 1;
int dif = 0, prev = -1;
int canuse = 0;
int cur = m;
int used = 0;
while(cur != t){
canuse++;
int tmp = 0;
for(auto x : adj[cur]){
if(x == prev) continue;
if(dp[x] - dif + used > mid){
if(!canuse) ok = 0;
else canuse--, tmp++;
}
}
used += tmp;
dif += adj[cur].size() - (prev != -1);
prev = cur;
cur = par[cur];
}
ttld = dif;
if(ok) r = mid;
else l = mid;
}
cout<<r + ttld<<"\n";
}
/*
24 1 12
1 4
4 5
5 6
6 7
5 8
8 9
8 10
10 11
10 12
10 13
8 14
14 15
15 18
15 16
15 17
14 19
19 20
19 21
11 2
13 3
9 22
9 23
9 24
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
7 ms |
27044 KB |
Output is correct |
4 |
Correct |
6 ms |
26972 KB |
Output is correct |
5 |
Correct |
6 ms |
27088 KB |
Output is correct |
6 |
Correct |
6 ms |
26972 KB |
Output is correct |
7 |
Correct |
6 ms |
27084 KB |
Output is correct |
8 |
Correct |
6 ms |
26976 KB |
Output is correct |
9 |
Correct |
6 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
27100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
92020 KB |
Output is correct |
2 |
Correct |
196 ms |
86612 KB |
Output is correct |
3 |
Correct |
535 ms |
90208 KB |
Output is correct |
4 |
Correct |
230 ms |
60500 KB |
Output is correct |
5 |
Correct |
544 ms |
90212 KB |
Output is correct |
6 |
Correct |
565 ms |
90144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
7 ms |
27044 KB |
Output is correct |
4 |
Correct |
6 ms |
26972 KB |
Output is correct |
5 |
Correct |
6 ms |
27088 KB |
Output is correct |
6 |
Correct |
6 ms |
26972 KB |
Output is correct |
7 |
Correct |
6 ms |
27084 KB |
Output is correct |
8 |
Correct |
6 ms |
26976 KB |
Output is correct |
9 |
Correct |
6 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
27100 KB |
Output is correct |
11 |
Correct |
6 ms |
26972 KB |
Output is correct |
12 |
Correct |
6 ms |
26972 KB |
Output is correct |
13 |
Correct |
6 ms |
26972 KB |
Output is correct |
14 |
Correct |
7 ms |
27228 KB |
Output is correct |
15 |
Correct |
6 ms |
27216 KB |
Output is correct |
16 |
Correct |
7 ms |
26972 KB |
Output is correct |
17 |
Correct |
6 ms |
26972 KB |
Output is correct |
18 |
Correct |
6 ms |
26972 KB |
Output is correct |
19 |
Correct |
7 ms |
26972 KB |
Output is correct |
20 |
Correct |
7 ms |
27100 KB |
Output is correct |
21 |
Correct |
7 ms |
27156 KB |
Output is correct |
22 |
Correct |
6 ms |
26972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26972 KB |
Output is correct |
2 |
Correct |
6 ms |
26972 KB |
Output is correct |
3 |
Correct |
7 ms |
27044 KB |
Output is correct |
4 |
Correct |
6 ms |
26972 KB |
Output is correct |
5 |
Correct |
6 ms |
27088 KB |
Output is correct |
6 |
Correct |
6 ms |
26972 KB |
Output is correct |
7 |
Correct |
6 ms |
27084 KB |
Output is correct |
8 |
Correct |
6 ms |
26976 KB |
Output is correct |
9 |
Correct |
6 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
27100 KB |
Output is correct |
11 |
Correct |
224 ms |
92020 KB |
Output is correct |
12 |
Correct |
196 ms |
86612 KB |
Output is correct |
13 |
Correct |
535 ms |
90208 KB |
Output is correct |
14 |
Correct |
230 ms |
60500 KB |
Output is correct |
15 |
Correct |
544 ms |
90212 KB |
Output is correct |
16 |
Correct |
565 ms |
90144 KB |
Output is correct |
17 |
Correct |
6 ms |
26972 KB |
Output is correct |
18 |
Correct |
6 ms |
26972 KB |
Output is correct |
19 |
Correct |
6 ms |
26972 KB |
Output is correct |
20 |
Correct |
7 ms |
27228 KB |
Output is correct |
21 |
Correct |
6 ms |
27216 KB |
Output is correct |
22 |
Correct |
7 ms |
26972 KB |
Output is correct |
23 |
Correct |
6 ms |
26972 KB |
Output is correct |
24 |
Correct |
6 ms |
26972 KB |
Output is correct |
25 |
Correct |
7 ms |
26972 KB |
Output is correct |
26 |
Correct |
7 ms |
27100 KB |
Output is correct |
27 |
Correct |
7 ms |
27156 KB |
Output is correct |
28 |
Correct |
6 ms |
26972 KB |
Output is correct |
29 |
Correct |
6 ms |
27104 KB |
Output is correct |
30 |
Correct |
213 ms |
91984 KB |
Output is correct |
31 |
Correct |
218 ms |
91992 KB |
Output is correct |
32 |
Correct |
332 ms |
162648 KB |
Output is correct |
33 |
Correct |
275 ms |
162468 KB |
Output is correct |
34 |
Correct |
537 ms |
90212 KB |
Output is correct |
35 |
Correct |
522 ms |
89972 KB |
Output is correct |
36 |
Correct |
697 ms |
98108 KB |
Output is correct |
37 |
Correct |
729 ms |
98008 KB |
Output is correct |
38 |
Correct |
485 ms |
102224 KB |
Output is correct |
39 |
Correct |
452 ms |
102252 KB |
Output is correct |
40 |
Correct |
464 ms |
102484 KB |
Output is correct |