#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, t, m, c;
vector<int> graph[1000001], chain;
ll dp[1000001], pref[1000001];
bool in_chain[1000001];
bool dfs(int node = t, int parent = 0) {
ll mx1 = 0, mx2 = 0;
for (int i : graph[node]) if (i != parent) {
in_chain[node] |= dfs(i, node);
if (dp[i] >= mx1) mx2 = mx1, mx1 = dp[i];
else if (dp[i] > mx2) mx2 = dp[i];
}
sort(graph[node].begin(), graph[node].end(), [](int A, int B) { return dp[A] > dp[B]; });
dp[node] = graph[node].size() - 1 + mx2;
if (in_chain[node]) chain.push_back(node);
return in_chain[node];
}
bool check(int mn) {
int rem = 1, used = 0;
for (int i = c - 1; i; i--, rem++) {
int allowed = mn - pref[i] - used;
for (int j : graph[chain[i]]) if (!in_chain[j]) {
if (dp[j] <= allowed) break;
if (!rem) return false;
rem--, used++;
}
}
return true;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> t >> m;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
in_chain[m] = true;
dfs();
reverse(chain.begin(), chain.end());
c = chain.size();
for (int i = 1; i < c; i++)
pref[i] = pref[i - 1] + graph[chain[i]].size() - 2 + (i == c - 1);
int l = pref[c - 1], r = n - 1;
while (l != r) {
int mid = (l + r) / 2;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
18 ms |
23788 KB |
Output is correct |
9 |
Correct |
17 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
348 ms |
63560 KB |
Output is correct |
2 |
Correct |
318 ms |
71532 KB |
Output is correct |
3 |
Correct |
975 ms |
78448 KB |
Output is correct |
4 |
Correct |
448 ms |
50852 KB |
Output is correct |
5 |
Correct |
943 ms |
78444 KB |
Output is correct |
6 |
Correct |
936 ms |
78572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
18 ms |
23788 KB |
Output is correct |
9 |
Correct |
17 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23788 KB |
Output is correct |
11 |
Correct |
15 ms |
23788 KB |
Output is correct |
12 |
Correct |
16 ms |
23916 KB |
Output is correct |
13 |
Correct |
16 ms |
23916 KB |
Output is correct |
14 |
Correct |
16 ms |
24044 KB |
Output is correct |
15 |
Correct |
15 ms |
24044 KB |
Output is correct |
16 |
Correct |
15 ms |
23916 KB |
Output is correct |
17 |
Correct |
15 ms |
23916 KB |
Output is correct |
18 |
Correct |
16 ms |
23916 KB |
Output is correct |
19 |
Correct |
15 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
15 ms |
23916 KB |
Output is correct |
22 |
Correct |
16 ms |
23916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23788 KB |
Output is correct |
2 |
Correct |
15 ms |
23788 KB |
Output is correct |
3 |
Correct |
15 ms |
23788 KB |
Output is correct |
4 |
Correct |
15 ms |
23788 KB |
Output is correct |
5 |
Correct |
15 ms |
23788 KB |
Output is correct |
6 |
Correct |
15 ms |
23808 KB |
Output is correct |
7 |
Correct |
15 ms |
23788 KB |
Output is correct |
8 |
Correct |
18 ms |
23788 KB |
Output is correct |
9 |
Correct |
17 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23788 KB |
Output is correct |
11 |
Correct |
348 ms |
63560 KB |
Output is correct |
12 |
Correct |
318 ms |
71532 KB |
Output is correct |
13 |
Correct |
975 ms |
78448 KB |
Output is correct |
14 |
Correct |
448 ms |
50852 KB |
Output is correct |
15 |
Correct |
943 ms |
78444 KB |
Output is correct |
16 |
Correct |
936 ms |
78572 KB |
Output is correct |
17 |
Correct |
15 ms |
23788 KB |
Output is correct |
18 |
Correct |
16 ms |
23916 KB |
Output is correct |
19 |
Correct |
16 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
24044 KB |
Output is correct |
21 |
Correct |
15 ms |
24044 KB |
Output is correct |
22 |
Correct |
15 ms |
23916 KB |
Output is correct |
23 |
Correct |
15 ms |
23916 KB |
Output is correct |
24 |
Correct |
16 ms |
23916 KB |
Output is correct |
25 |
Correct |
15 ms |
23916 KB |
Output is correct |
26 |
Correct |
16 ms |
23916 KB |
Output is correct |
27 |
Correct |
15 ms |
23916 KB |
Output is correct |
28 |
Correct |
16 ms |
23916 KB |
Output is correct |
29 |
Correct |
15 ms |
23820 KB |
Output is correct |
30 |
Correct |
386 ms |
76908 KB |
Output is correct |
31 |
Correct |
365 ms |
77032 KB |
Output is correct |
32 |
Correct |
510 ms |
183508 KB |
Output is correct |
33 |
Correct |
427 ms |
171432 KB |
Output is correct |
34 |
Correct |
923 ms |
78404 KB |
Output is correct |
35 |
Correct |
928 ms |
78316 KB |
Output is correct |
36 |
Correct |
1008 ms |
88848 KB |
Output is correct |
37 |
Correct |
1016 ms |
89068 KB |
Output is correct |
38 |
Correct |
824 ms |
90980 KB |
Output is correct |
39 |
Correct |
838 ms |
90984 KB |
Output is correct |
40 |
Correct |
842 ms |
91188 KB |
Output is correct |