#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int maxn = 1000000;
int n, m, k;
int a[maxn], d[maxn], dp[maxn];
vector<int> graph[maxn];
vector<pi> v;
void dfs(int c, int p){
pi ret;
for(int i : graph[c]){
if(i == p) continue;
if(p != -1) dp[i] += dp[c] + graph[c].size() - 2;
dfs(i, c);
a[c] |= a[i];
ret.s = max(ret.s, dp[i]);
if(ret.s > ret.f) swap(ret.f, ret.s);
}
if(graph[c].size() > 2) dp[c] = ret.s;
dp[c] += graph[c].size() > 1;
}
void dfs2(int c, int p){
if(c == m) return;
for(int i : graph[c]){
if(i == p) continue;
d[i] = d[c] + 1;
dfs2(i, c);
if(a[c] && !a[i]) v.push_back({d[i], dp[i] + (c == k)});
}
}
bool works(int x){
int ret = 0;
for(pi i : v){
if(ret > i.f) return 0;
ret += (i.s + ret) > x;
}
return ret <= x;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
m--, k--;
for(int i = 0; i < n - 1; i++){
int u, v;
cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
a[k] = 1, d[k] = -1;
dfs(m, -1);
dfs2(k, -1);
sort(v.begin(), v.end());
int l = -1, r = n;
while(r - l > 1){
int mid = (l + r) / 2;
if(works(mid)) r = mid;
else l = mid;
}
cout << r << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
15 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
15 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
14 ms |
23808 KB |
Output is correct |
9 |
Correct |
17 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
78340 KB |
Output is correct |
2 |
Correct |
382 ms |
72824 KB |
Output is correct |
3 |
Correct |
1033 ms |
81232 KB |
Output is correct |
4 |
Correct |
465 ms |
52472 KB |
Output is correct |
5 |
Correct |
993 ms |
81364 KB |
Output is correct |
6 |
Correct |
998 ms |
81252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
15 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
15 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
14 ms |
23808 KB |
Output is correct |
9 |
Correct |
17 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
14 ms |
23808 KB |
Output is correct |
12 |
Correct |
14 ms |
23936 KB |
Output is correct |
13 |
Correct |
14 ms |
23936 KB |
Output is correct |
14 |
Correct |
15 ms |
23936 KB |
Output is correct |
15 |
Correct |
15 ms |
23936 KB |
Output is correct |
16 |
Correct |
14 ms |
24064 KB |
Output is correct |
17 |
Correct |
15 ms |
23936 KB |
Output is correct |
18 |
Correct |
15 ms |
23936 KB |
Output is correct |
19 |
Correct |
15 ms |
23936 KB |
Output is correct |
20 |
Correct |
15 ms |
23864 KB |
Output is correct |
21 |
Correct |
15 ms |
23936 KB |
Output is correct |
22 |
Correct |
14 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
15 ms |
23936 KB |
Output is correct |
3 |
Correct |
17 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
15 ms |
23808 KB |
Output is correct |
6 |
Correct |
19 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
14 ms |
23808 KB |
Output is correct |
9 |
Correct |
17 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
425 ms |
78340 KB |
Output is correct |
12 |
Correct |
382 ms |
72824 KB |
Output is correct |
13 |
Correct |
1033 ms |
81232 KB |
Output is correct |
14 |
Correct |
465 ms |
52472 KB |
Output is correct |
15 |
Correct |
993 ms |
81364 KB |
Output is correct |
16 |
Correct |
998 ms |
81252 KB |
Output is correct |
17 |
Correct |
14 ms |
23808 KB |
Output is correct |
18 |
Correct |
14 ms |
23936 KB |
Output is correct |
19 |
Correct |
14 ms |
23936 KB |
Output is correct |
20 |
Correct |
15 ms |
23936 KB |
Output is correct |
21 |
Correct |
15 ms |
23936 KB |
Output is correct |
22 |
Correct |
14 ms |
24064 KB |
Output is correct |
23 |
Correct |
15 ms |
23936 KB |
Output is correct |
24 |
Correct |
15 ms |
23936 KB |
Output is correct |
25 |
Correct |
15 ms |
23936 KB |
Output is correct |
26 |
Correct |
15 ms |
23864 KB |
Output is correct |
27 |
Correct |
15 ms |
23936 KB |
Output is correct |
28 |
Correct |
14 ms |
23928 KB |
Output is correct |
29 |
Correct |
14 ms |
23808 KB |
Output is correct |
30 |
Correct |
402 ms |
78328 KB |
Output is correct |
31 |
Correct |
400 ms |
78332 KB |
Output is correct |
32 |
Correct |
493 ms |
174364 KB |
Output is correct |
33 |
Correct |
515 ms |
174456 KB |
Output is correct |
34 |
Correct |
997 ms |
81208 KB |
Output is correct |
35 |
Correct |
988 ms |
81240 KB |
Output is correct |
36 |
Correct |
999 ms |
93036 KB |
Output is correct |
37 |
Correct |
989 ms |
92984 KB |
Output is correct |
38 |
Correct |
769 ms |
91500 KB |
Output is correct |
39 |
Correct |
805 ms |
91380 KB |
Output is correct |
40 |
Correct |
829 ms |
91500 KB |
Output is correct |