#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 1e6 + 5;
const long long INF = 1ll << 60;
int F[N], n, T, M, ch[N], pr[N], bala[N];
vector<int> adj[N];
void dfs2(int v, int p) {
for (auto u: adj[v]) if (u != p) {
bala[u] = bala[v] + ch[v] - 1;
dfs2(u, v);
}
}
void dfs(int v, int p) {
pr[v] = p;
vector<int> tmp;
for (auto u: adj[v]) if (u != p) {
dfs(u, v);
tmp.push_back(F[u]);
ch[v]++;
}
sort(tmp.rbegin(), tmp.rend());
if (tmp.size() <= 1)
F[v] = tmp.size();
else
F[v] = tmp[1] + tmp.size();
}
bool check(int O) {
int m = M;
int child = -1;
int cur = 0;
while (m != T) {
int d = 0;
for (auto u: adj[m]) if (u != pr[m] && u != child) {
int x = F[u] + bala[u] + (m == M);
if (x >= O) {
cur--;
d--;
}
}
cur++;
if (cur < 0) return false;
O += d;
child = m;
m = pr[m];
}
return O > 0;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> T >> M;
T--, M--;
for (int i = 1; i < n; i++) {
int u, v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
if (M == T) {
cout << 0 << '\n';
return 0;
}
dfs(T, T);
ch[T] = 1;
dfs2(T, T);
for (int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end(), [&](const int x, const int y) {return F[x] + bala[x] > F[y] + bala[y];});
}
int l = 0, r = INF;
while (r - l > 1) {
int m = l + r >> 1;
if (check(m)) r = m;
else l = m;
}
cout << l << '\n';
return 0;
}
Compilation message
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:79:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
79 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
358 ms |
90340 KB |
Output is correct |
2 |
Correct |
292 ms |
83684 KB |
Output is correct |
3 |
Correct |
975 ms |
92508 KB |
Output is correct |
4 |
Correct |
489 ms |
58184 KB |
Output is correct |
5 |
Correct |
982 ms |
92624 KB |
Output is correct |
6 |
Correct |
1014 ms |
92680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
14 ms |
23892 KB |
Output is correct |
13 |
Correct |
13 ms |
23812 KB |
Output is correct |
14 |
Correct |
13 ms |
24020 KB |
Output is correct |
15 |
Correct |
16 ms |
24008 KB |
Output is correct |
16 |
Correct |
15 ms |
23892 KB |
Output is correct |
17 |
Correct |
13 ms |
23816 KB |
Output is correct |
18 |
Correct |
12 ms |
23816 KB |
Output is correct |
19 |
Correct |
14 ms |
23820 KB |
Output is correct |
20 |
Correct |
13 ms |
23884 KB |
Output is correct |
21 |
Correct |
12 ms |
23892 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23816 KB |
Output is correct |
3 |
Correct |
13 ms |
23776 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
12 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
12 ms |
23812 KB |
Output is correct |
11 |
Correct |
358 ms |
90340 KB |
Output is correct |
12 |
Correct |
292 ms |
83684 KB |
Output is correct |
13 |
Correct |
975 ms |
92508 KB |
Output is correct |
14 |
Correct |
489 ms |
58184 KB |
Output is correct |
15 |
Correct |
982 ms |
92624 KB |
Output is correct |
16 |
Correct |
1014 ms |
92680 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
14 ms |
23892 KB |
Output is correct |
19 |
Correct |
13 ms |
23812 KB |
Output is correct |
20 |
Correct |
13 ms |
24020 KB |
Output is correct |
21 |
Correct |
16 ms |
24008 KB |
Output is correct |
22 |
Correct |
15 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23816 KB |
Output is correct |
24 |
Correct |
12 ms |
23816 KB |
Output is correct |
25 |
Correct |
14 ms |
23820 KB |
Output is correct |
26 |
Correct |
13 ms |
23884 KB |
Output is correct |
27 |
Correct |
12 ms |
23892 KB |
Output is correct |
28 |
Correct |
12 ms |
23892 KB |
Output is correct |
29 |
Correct |
12 ms |
23808 KB |
Output is correct |
30 |
Correct |
389 ms |
103568 KB |
Output is correct |
31 |
Correct |
323 ms |
103576 KB |
Output is correct |
32 |
Correct |
586 ms |
225196 KB |
Output is correct |
33 |
Correct |
399 ms |
225172 KB |
Output is correct |
34 |
Correct |
981 ms |
105780 KB |
Output is correct |
35 |
Correct |
1042 ms |
105728 KB |
Output is correct |
36 |
Correct |
1357 ms |
118264 KB |
Output is correct |
37 |
Correct |
1350 ms |
118296 KB |
Output is correct |
38 |
Correct |
966 ms |
116428 KB |
Output is correct |
39 |
Correct |
945 ms |
116428 KB |
Output is correct |
40 |
Correct |
844 ms |
116340 KB |
Output is correct |