#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 |
23788 KB |
Output is correct |
7 |
Correct |
18 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
341 ms |
63468 KB |
Output is correct |
2 |
Correct |
327 ms |
59472 KB |
Output is correct |
3 |
Correct |
988 ms |
65004 KB |
Output is correct |
4 |
Correct |
447 ms |
44396 KB |
Output is correct |
5 |
Correct |
929 ms |
65152 KB |
Output is correct |
6 |
Correct |
908 ms |
65004 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 |
23788 KB |
Output is correct |
7 |
Correct |
18 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
18 ms |
23788 KB |
Output is correct |
12 |
Correct |
18 ms |
23916 KB |
Output is correct |
13 |
Correct |
16 ms |
23916 KB |
Output is correct |
14 |
Correct |
15 ms |
24044 KB |
Output is correct |
15 |
Correct |
16 ms |
23916 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 |
16 ms |
23916 KB |
Output is correct |
20 |
Correct |
16 ms |
23916 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 KB |
Output is correct |
22 |
Correct |
15 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 |
23788 KB |
Output is correct |
7 |
Correct |
18 ms |
23788 KB |
Output is correct |
8 |
Correct |
15 ms |
23788 KB |
Output is correct |
9 |
Correct |
15 ms |
23788 KB |
Output is correct |
10 |
Correct |
15 ms |
23788 KB |
Output is correct |
11 |
Correct |
341 ms |
63468 KB |
Output is correct |
12 |
Correct |
327 ms |
59472 KB |
Output is correct |
13 |
Correct |
988 ms |
65004 KB |
Output is correct |
14 |
Correct |
447 ms |
44396 KB |
Output is correct |
15 |
Correct |
929 ms |
65152 KB |
Output is correct |
16 |
Correct |
908 ms |
65004 KB |
Output is correct |
17 |
Correct |
18 ms |
23788 KB |
Output is correct |
18 |
Correct |
18 ms |
23916 KB |
Output is correct |
19 |
Correct |
16 ms |
23916 KB |
Output is correct |
20 |
Correct |
15 ms |
24044 KB |
Output is correct |
21 |
Correct |
16 ms |
23916 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 |
16 ms |
23916 KB |
Output is correct |
26 |
Correct |
16 ms |
23916 KB |
Output is correct |
27 |
Correct |
16 ms |
23916 KB |
Output is correct |
28 |
Correct |
15 ms |
23916 KB |
Output is correct |
29 |
Correct |
16 ms |
23788 KB |
Output is correct |
30 |
Correct |
385 ms |
63564 KB |
Output is correct |
31 |
Correct |
350 ms |
63508 KB |
Output is correct |
32 |
Correct |
498 ms |
169688 KB |
Output is correct |
33 |
Correct |
402 ms |
157804 KB |
Output is correct |
34 |
Correct |
922 ms |
65260 KB |
Output is correct |
35 |
Correct |
919 ms |
65116 KB |
Output is correct |
36 |
Correct |
1000 ms |
75876 KB |
Output is correct |
37 |
Correct |
1052 ms |
75748 KB |
Output is correct |
38 |
Correct |
813 ms |
77668 KB |
Output is correct |
39 |
Correct |
836 ms |
77576 KB |
Output is correct |
40 |
Correct |
858 ms |
77796 KB |
Output is correct |