#include <bits/stdc++.h>
using namespace std;
const int INF = INT32_MAX;
int N, T, M, P[1000005], rem[1000005];
int DP[1000005], deg[1000005], S[1000005];
vector<int> adj[1000005], C[1000005];
void dfs(int v, int p) {
if(v == M) rem[v] = 1;
for(auto u : adj[v]) {
if(u == p) continue;
dfs(u, v); P[u] = v;
if(rem[u]) rem[v] = 1;
else {
C[v].push_back(u); ++deg[v];
}
}
sort(C[v].begin(), C[v].end(), [](int A, int B) { return DP[A] > DP[B]; });
if(C[v].size() <= 1) DP[v] = deg[v];
else DP[v] = deg[v] + DP[C[v][1]];
}
void dfs(int v) {
DP[v] += S[v];
for(auto u : adj[v]) {
if(u == P[v]) continue;
S[u] = S[v] + deg[v]; dfs(u);
}
}
bool possible(int v) {
int cur = 0, dep = 0;
for(int e = M; e != T; e = P[e]) {
++dep; int k = 0;
for(auto u : C[e]) {
if(DP[u] + cur - k > v) ++cur, ++k;
}
if(cur > dep || cur > v)
return false;
}
return true;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> T >> M;
for(int l = 0; l < N - 1; l++) {
int U, V; cin >> U >> V;
adj[U].push_back(V);
adj[V].push_back(U);
}
dfs(T, T); deg[T] = 0; dfs(T);
int lo = 0, hi = N, ans = -1;
while(lo <= hi) {
int md = (lo + hi) / 2;
if(possible(md)) ans = md, hi = md - 1;
else lo = md + 1;
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47316 KB |
Output is correct |
2 |
Correct |
23 ms |
47240 KB |
Output is correct |
3 |
Correct |
24 ms |
47200 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
27 ms |
47212 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47208 KB |
Output is correct |
8 |
Correct |
24 ms |
47296 KB |
Output is correct |
9 |
Correct |
23 ms |
47224 KB |
Output is correct |
10 |
Correct |
24 ms |
47296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
357 ms |
108452 KB |
Output is correct |
2 |
Correct |
323 ms |
102488 KB |
Output is correct |
3 |
Correct |
986 ms |
112080 KB |
Output is correct |
4 |
Correct |
474 ms |
85904 KB |
Output is correct |
5 |
Correct |
1006 ms |
124712 KB |
Output is correct |
6 |
Correct |
1008 ms |
124728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47316 KB |
Output is correct |
2 |
Correct |
23 ms |
47240 KB |
Output is correct |
3 |
Correct |
24 ms |
47200 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
27 ms |
47212 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47208 KB |
Output is correct |
8 |
Correct |
24 ms |
47296 KB |
Output is correct |
9 |
Correct |
23 ms |
47224 KB |
Output is correct |
10 |
Correct |
24 ms |
47296 KB |
Output is correct |
11 |
Correct |
24 ms |
47216 KB |
Output is correct |
12 |
Correct |
26 ms |
47300 KB |
Output is correct |
13 |
Correct |
26 ms |
47316 KB |
Output is correct |
14 |
Correct |
24 ms |
47436 KB |
Output is correct |
15 |
Correct |
25 ms |
47428 KB |
Output is correct |
16 |
Correct |
24 ms |
47296 KB |
Output is correct |
17 |
Correct |
24 ms |
47300 KB |
Output is correct |
18 |
Correct |
24 ms |
47276 KB |
Output is correct |
19 |
Correct |
24 ms |
47268 KB |
Output is correct |
20 |
Correct |
24 ms |
47344 KB |
Output is correct |
21 |
Correct |
24 ms |
47380 KB |
Output is correct |
22 |
Correct |
24 ms |
47300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47316 KB |
Output is correct |
2 |
Correct |
23 ms |
47240 KB |
Output is correct |
3 |
Correct |
24 ms |
47200 KB |
Output is correct |
4 |
Correct |
24 ms |
47308 KB |
Output is correct |
5 |
Correct |
27 ms |
47212 KB |
Output is correct |
6 |
Correct |
23 ms |
47316 KB |
Output is correct |
7 |
Correct |
25 ms |
47208 KB |
Output is correct |
8 |
Correct |
24 ms |
47296 KB |
Output is correct |
9 |
Correct |
23 ms |
47224 KB |
Output is correct |
10 |
Correct |
24 ms |
47296 KB |
Output is correct |
11 |
Correct |
357 ms |
108452 KB |
Output is correct |
12 |
Correct |
323 ms |
102488 KB |
Output is correct |
13 |
Correct |
986 ms |
112080 KB |
Output is correct |
14 |
Correct |
474 ms |
85904 KB |
Output is correct |
15 |
Correct |
1006 ms |
124712 KB |
Output is correct |
16 |
Correct |
1008 ms |
124728 KB |
Output is correct |
17 |
Correct |
24 ms |
47216 KB |
Output is correct |
18 |
Correct |
26 ms |
47300 KB |
Output is correct |
19 |
Correct |
26 ms |
47316 KB |
Output is correct |
20 |
Correct |
24 ms |
47436 KB |
Output is correct |
21 |
Correct |
25 ms |
47428 KB |
Output is correct |
22 |
Correct |
24 ms |
47296 KB |
Output is correct |
23 |
Correct |
24 ms |
47300 KB |
Output is correct |
24 |
Correct |
24 ms |
47276 KB |
Output is correct |
25 |
Correct |
24 ms |
47268 KB |
Output is correct |
26 |
Correct |
24 ms |
47344 KB |
Output is correct |
27 |
Correct |
24 ms |
47380 KB |
Output is correct |
28 |
Correct |
24 ms |
47300 KB |
Output is correct |
29 |
Correct |
28 ms |
47288 KB |
Output is correct |
30 |
Correct |
404 ms |
121364 KB |
Output is correct |
31 |
Correct |
364 ms |
121296 KB |
Output is correct |
32 |
Correct |
467 ms |
186044 KB |
Output is correct |
33 |
Correct |
388 ms |
217320 KB |
Output is correct |
34 |
Correct |
1074 ms |
124792 KB |
Output is correct |
35 |
Correct |
1086 ms |
124872 KB |
Output is correct |
36 |
Correct |
999 ms |
132448 KB |
Output is correct |
37 |
Correct |
1018 ms |
132456 KB |
Output is correct |
38 |
Correct |
810 ms |
124320 KB |
Output is correct |
39 |
Correct |
680 ms |
124292 KB |
Output is correct |
40 |
Correct |
707 ms |
124228 KB |
Output is correct |