#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
const int N = 1e6 + 5;
vector<int> edges[N];
int n, t, m, dp[N], is[N];
int c[N];
void dfs(int u, int p = -1){
is[u] = (u == t), c[u] = -1;
vector<int> t;
for(auto x : edges[u]){
if(x == p) continue;
dfs(x, u);
is[u] |= is[x];
if(!is[x]) t.push_back(x);
else c[u] = x;
}
sort(t.begin(), t.end(), [&](int& i, int& j){
return dp[i] > dp[j];
});
edges[u] = t;
if(t.empty()) dp[u] = 0;
else {
dp[u] = t.size();
if((int)t.size() > 1) dp[u] += dp[t[1]];
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>t>>m;
if(t == m){
cout<<0<<"\n";
return 0;
}
for(int i=1;i<n;i++){
int a, b; cin>>a>>b;
edges[a].push_back(b);
edges[b].push_back(a);
}
dfs(m);
int tot = 0, u = m;
while(u != t){
tot += edges[u].size();
u = c[u];
}
auto check = [&](int mx){
int u = m, pos = 0;
while(u != t){
pos++;
for(auto x : edges[u]){
if(dp[x] + tot > mx) pos--;
}
if(pos < 0) return false;
u = c[u];
}
return true;
};
int l = tot, r = 1e9;
while(l < r){
int m = (l + r) >> 1;
if(check(m)) r = m;
else l = m + 1;
}
//~ while(m != t){
//~ pos++;
//~ vector<int>& t = edges[m];
//~ sort(t.begin(), t.end(), [&](int& i, int& j){
//~ return dp[i] > dp[j];
//~ });
//~ if(pos >= (int)t.size()){
//~ pos -= (int)t.size();
//~ res += (int)t.size();
//~ } else {
//~ res += dp[t[pos]] + (int)t.size();
//~ pos = 1e9;
//~ }
//~ m = c[m];
//~ }
cout<<l<<"\n";
}
/*
8 7 1
1 2
2 3
3 4
3 5
3 6
4 7
4 8
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23760 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23820 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
13 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23812 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
68172 KB |
Output is correct |
2 |
Correct |
277 ms |
63820 KB |
Output is correct |
3 |
Correct |
778 ms |
69248 KB |
Output is correct |
4 |
Correct |
368 ms |
47288 KB |
Output is correct |
5 |
Correct |
764 ms |
69312 KB |
Output is correct |
6 |
Correct |
807 ms |
69208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23760 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23820 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
13 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23812 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23816 KB |
Output is correct |
14 |
Correct |
12 ms |
23892 KB |
Output is correct |
15 |
Correct |
12 ms |
23892 KB |
Output is correct |
16 |
Incorrect |
14 ms |
23804 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23760 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
14 ms |
23820 KB |
Output is correct |
4 |
Correct |
13 ms |
23808 KB |
Output is correct |
5 |
Correct |
12 ms |
23812 KB |
Output is correct |
6 |
Correct |
13 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23828 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
12 ms |
23812 KB |
Output is correct |
10 |
Correct |
14 ms |
23764 KB |
Output is correct |
11 |
Correct |
299 ms |
68172 KB |
Output is correct |
12 |
Correct |
277 ms |
63820 KB |
Output is correct |
13 |
Correct |
778 ms |
69248 KB |
Output is correct |
14 |
Correct |
368 ms |
47288 KB |
Output is correct |
15 |
Correct |
764 ms |
69312 KB |
Output is correct |
16 |
Correct |
807 ms |
69208 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23816 KB |
Output is correct |
20 |
Correct |
12 ms |
23892 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Incorrect |
14 ms |
23804 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |