#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, S, T;
vector <int> g[N];
int f[N], flag[N];
int cnt;
void dfs(int u, int p) {
int best[3] = {0, 0, 0};
for (auto v: g[u]) {
if (v == p) continue;
dfs(v, u);
best[2] = f[v];
sort(best, best + 3);
reverse(best, best + 3);
}
f[u] = best[1] + g[u].size() - 1;
}
int par[N];
void init(int u, int p) {
par[u] = p;
for (auto v: g[u]) {
if (v == p) continue;
init(v, u);
}
}
vector <int> s[N];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> T >> S;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
init(S, 0);
vector <int> path;
for (int u = T; u != S; u = par[u]) flag[par[u]] = 1, path.push_back(par[u]);
int need = 0;
for (auto u: path) {
for (auto v: g[u]) {
if (flag[v]) continue;
if (v == T) continue;
dfs(v, u);
s[u].push_back(f[v]);
}
sort(s[u].begin(), s[u].end());
for (auto& x: s[u]) x += ++need;
reverse(s[u].begin(), s[u].end());
}
reverse(path.begin(), path.end());
auto check = [&](int limit) {
int k = 0, tot = 0;
for (auto u: path) {
++k;
int i = 0;
while (i < s[u].size() && s[u][i] + tot > limit) {
--k;
if (k < 0) return 0;
++tot;
++i;
}
}
if (tot > limit) return 0;
return 1;
};
int L = 0, R = n;
while (L < R) {
int mid = L + R >> 1;
if (check(mid)) R = mid;
else L = mid + 1;
}
cout << L;
}
Compilation message
mousetrap.cpp: In lambda function:
mousetrap.cpp:67:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | while (i < s[u].size() && s[u][i] + tot > limit) {
| ~~^~~~~~~~~~~~~
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:79:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
79 | int mid = L + R >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47176 KB |
Output is correct |
2 |
Correct |
26 ms |
47236 KB |
Output is correct |
3 |
Correct |
25 ms |
47188 KB |
Output is correct |
4 |
Correct |
25 ms |
47292 KB |
Output is correct |
5 |
Correct |
29 ms |
47172 KB |
Output is correct |
6 |
Correct |
26 ms |
47264 KB |
Output is correct |
7 |
Correct |
26 ms |
47180 KB |
Output is correct |
8 |
Correct |
30 ms |
47284 KB |
Output is correct |
9 |
Correct |
29 ms |
47188 KB |
Output is correct |
10 |
Correct |
26 ms |
47180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
309 ms |
86412 KB |
Output is correct |
2 |
Correct |
280 ms |
82508 KB |
Output is correct |
3 |
Correct |
853 ms |
87400 KB |
Output is correct |
4 |
Correct |
392 ms |
67420 KB |
Output is correct |
5 |
Correct |
851 ms |
87568 KB |
Output is correct |
6 |
Correct |
864 ms |
87516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47176 KB |
Output is correct |
2 |
Correct |
26 ms |
47236 KB |
Output is correct |
3 |
Correct |
25 ms |
47188 KB |
Output is correct |
4 |
Correct |
25 ms |
47292 KB |
Output is correct |
5 |
Correct |
29 ms |
47172 KB |
Output is correct |
6 |
Correct |
26 ms |
47264 KB |
Output is correct |
7 |
Correct |
26 ms |
47180 KB |
Output is correct |
8 |
Correct |
30 ms |
47284 KB |
Output is correct |
9 |
Correct |
29 ms |
47188 KB |
Output is correct |
10 |
Correct |
26 ms |
47180 KB |
Output is correct |
11 |
Correct |
27 ms |
47200 KB |
Output is correct |
12 |
Correct |
32 ms |
47292 KB |
Output is correct |
13 |
Correct |
34 ms |
47316 KB |
Output is correct |
14 |
Correct |
27 ms |
47336 KB |
Output is correct |
15 |
Correct |
26 ms |
47316 KB |
Output is correct |
16 |
Correct |
27 ms |
47216 KB |
Output is correct |
17 |
Correct |
29 ms |
47316 KB |
Output is correct |
18 |
Correct |
27 ms |
47300 KB |
Output is correct |
19 |
Correct |
28 ms |
47208 KB |
Output is correct |
20 |
Correct |
26 ms |
47260 KB |
Output is correct |
21 |
Correct |
28 ms |
47352 KB |
Output is correct |
22 |
Correct |
27 ms |
47344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
47176 KB |
Output is correct |
2 |
Correct |
26 ms |
47236 KB |
Output is correct |
3 |
Correct |
25 ms |
47188 KB |
Output is correct |
4 |
Correct |
25 ms |
47292 KB |
Output is correct |
5 |
Correct |
29 ms |
47172 KB |
Output is correct |
6 |
Correct |
26 ms |
47264 KB |
Output is correct |
7 |
Correct |
26 ms |
47180 KB |
Output is correct |
8 |
Correct |
30 ms |
47284 KB |
Output is correct |
9 |
Correct |
29 ms |
47188 KB |
Output is correct |
10 |
Correct |
26 ms |
47180 KB |
Output is correct |
11 |
Correct |
309 ms |
86412 KB |
Output is correct |
12 |
Correct |
280 ms |
82508 KB |
Output is correct |
13 |
Correct |
853 ms |
87400 KB |
Output is correct |
14 |
Correct |
392 ms |
67420 KB |
Output is correct |
15 |
Correct |
851 ms |
87568 KB |
Output is correct |
16 |
Correct |
864 ms |
87516 KB |
Output is correct |
17 |
Correct |
27 ms |
47200 KB |
Output is correct |
18 |
Correct |
32 ms |
47292 KB |
Output is correct |
19 |
Correct |
34 ms |
47316 KB |
Output is correct |
20 |
Correct |
27 ms |
47336 KB |
Output is correct |
21 |
Correct |
26 ms |
47316 KB |
Output is correct |
22 |
Correct |
27 ms |
47216 KB |
Output is correct |
23 |
Correct |
29 ms |
47316 KB |
Output is correct |
24 |
Correct |
27 ms |
47300 KB |
Output is correct |
25 |
Correct |
28 ms |
47208 KB |
Output is correct |
26 |
Correct |
26 ms |
47260 KB |
Output is correct |
27 |
Correct |
28 ms |
47352 KB |
Output is correct |
28 |
Correct |
27 ms |
47344 KB |
Output is correct |
29 |
Correct |
32 ms |
47188 KB |
Output is correct |
30 |
Correct |
343 ms |
86492 KB |
Output is correct |
31 |
Correct |
329 ms |
86352 KB |
Output is correct |
32 |
Correct |
364 ms |
114904 KB |
Output is correct |
33 |
Correct |
380 ms |
195936 KB |
Output is correct |
34 |
Correct |
953 ms |
87648 KB |
Output is correct |
35 |
Correct |
1020 ms |
87576 KB |
Output is correct |
36 |
Correct |
918 ms |
93116 KB |
Output is correct |
37 |
Correct |
930 ms |
93172 KB |
Output is correct |
38 |
Correct |
712 ms |
94892 KB |
Output is correct |
39 |
Correct |
706 ms |
94888 KB |
Output is correct |
40 |
Correct |
695 ms |
94816 KB |
Output is correct |