#include <bits/stdc++.h>
using std::vector;
template <class F> struct fixed : private F {
explicit fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, T, M;
std::cin >> N >> T >> M;
T -= 1, M -= 1;
vector<vector<int>> graph(N);
for (int e = 1; e < N; ++e) {
int a, b;
std::cin >> a >> b;
a -= 1, b -= 1;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<int> parent(N);
parent[T] = -1;
fixed([&](auto&& dfs, const int u) -> void {
for (const int v : graph[u]) {
if (v != parent[u]) {
parent[v] = u;
dfs(v);
}
}
})(T);
vector<int> path;
vector<char> on_path(N);
for (int u = M; u != T; u = parent[u]) {
path.push_back(u);
on_path[u] = true;
}
on_path[T] = true;
const int L = (int)path.size();
vector<vector<int>> dp(L);
for (int i = 0; i < L; ++i) {
for (const int src : graph[path[i]]) {
if (!on_path[src]) {
dp[i].push_back(fixed([&](auto&& dfs, const int u) -> int {
vector<int> list;
for (const int v : graph[u]) {
if (v != parent[u]) {
list.push_back(dfs(v));
}
}
const int n = (int)list.size();
std::sort(list.rbegin(), list.rend());
return (n >= 2 ? list[1] : 0) + n;
})(src));
}
}
}
vector<int> above(L);
for (int i = L - 1; i > 0; --i) {
above[i - 1] = above[i] + (int)graph[path[i]].size() - 2;
}
const auto check = [&](const int thres) {
int cut = 0;
for (int i = 0; i < L; ++i) {
const int offset = cut + above[i] + (int)dp[i].size();
for (const int x : dp[i]) {
if (offset + x > thres) {
if (cut > i) {
return false;
}
cut += 1;
}
}
}
return cut <= thres;
};
int ok = N, ng = -1;
while (ok - ng > 1) {
const int md = (ok + ng) / 2;
(check(md) ? ok : ng) = md;
}
std::cout << ok << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
63584 KB |
Output is correct |
2 |
Correct |
313 ms |
66016 KB |
Output is correct |
3 |
Correct |
968 ms |
74240 KB |
Output is correct |
4 |
Correct |
427 ms |
37132 KB |
Output is correct |
5 |
Correct |
960 ms |
74396 KB |
Output is correct |
6 |
Correct |
928 ms |
74244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
389 ms |
63584 KB |
Output is correct |
12 |
Correct |
313 ms |
66016 KB |
Output is correct |
13 |
Correct |
968 ms |
74240 KB |
Output is correct |
14 |
Correct |
427 ms |
37132 KB |
Output is correct |
15 |
Correct |
960 ms |
74396 KB |
Output is correct |
16 |
Correct |
928 ms |
74244 KB |
Output is correct |
17 |
Correct |
0 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
356 ms |
73284 KB |
Output is correct |
31 |
Correct |
354 ms |
73268 KB |
Output is correct |
32 |
Correct |
425 ms |
139296 KB |
Output is correct |
33 |
Correct |
447 ms |
198736 KB |
Output is correct |
34 |
Correct |
1034 ms |
74436 KB |
Output is correct |
35 |
Correct |
1046 ms |
74324 KB |
Output is correct |
36 |
Correct |
980 ms |
83704 KB |
Output is correct |
37 |
Correct |
1000 ms |
83744 KB |
Output is correct |
38 |
Correct |
799 ms |
85608 KB |
Output is correct |
39 |
Correct |
864 ms |
85544 KB |
Output is correct |
40 |
Correct |
783 ms |
85608 KB |
Output is correct |