#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
const int N = 1e6 + 5, INF = 1e9;
vector<int> g[N];
int n, m, t;
bool term[N];
vector<vector<int>> w;
void dive(int node, int anc) {
if (node == t)
term[node] = 1;
for (int to : g[node]) {
if (to != anc) {
dive(to, node);
if (term[to])
term[node] = 1;
}
}
}
int dfs(int node, int anc) {
int adj = g[node].size() - (anc != -1);
vector<int> v;
for (int to : g[node]) {
if (to != anc) {
v.push_back(dfs(to, node) + 1);
}
}
sort(v.rbegin(), v.rend());
if (v.empty())
return 0;
else if (v.size() == 1)
return 1;
return v[1] + adj - 1;
}
int walk(int node, int anc) {
if (node == t)
return 0;
vector<int> v;
for (int to : g[node]) {
if (to != anc && !term[to]) {
v.push_back(dfs(to, node) + 1);
}
}
int add = 0;
for (int to : g[node]) {
if (to != anc) {
if (term[to]) {
add = walk(to, node);
}
}
}
for (int &x : v)
x += v.size() - 1 + add;
sort(v.begin(), v.end());
w.push_back(v);
add += v.size();
return add;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> t >> m;
m--, t--;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
dive(m, -1);
walk(m, -1);
reverse(all(w));
// for (auto &v : w) {
// for (int x : v) {
// cout << x << " ";
// }
// cout << "\n";
// }
int l = -1, r = INF;
while (r - l > 1) {
int p = (l + r) >> 1;
bool ok = true;
int energy = 0, expended = 0;
for (auto &v : w) {
energy++;
for (int x : v) {
if (x + expended > p) {
if (energy == 0 || expended + 1 > p) {
ok = false;
break;
}
expended++;
energy--;
}
}
if (!ok)
break;
}
if (ok)
r = p;
else
l = p;
}
cout << r << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23772 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
26 ms |
23800 KB |
Output is correct |
10 |
Correct |
26 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
495 ms |
55288 KB |
Output is correct |
2 |
Correct |
471 ms |
52108 KB |
Output is correct |
3 |
Correct |
1159 ms |
56472 KB |
Output is correct |
4 |
Correct |
578 ms |
40212 KB |
Output is correct |
5 |
Correct |
1167 ms |
56180 KB |
Output is correct |
6 |
Correct |
1148 ms |
56388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23772 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
26 ms |
23800 KB |
Output is correct |
10 |
Correct |
26 ms |
23800 KB |
Output is correct |
11 |
Correct |
26 ms |
23800 KB |
Output is correct |
12 |
Correct |
24 ms |
23928 KB |
Output is correct |
13 |
Correct |
26 ms |
23904 KB |
Output is correct |
14 |
Correct |
28 ms |
24056 KB |
Output is correct |
15 |
Correct |
25 ms |
24056 KB |
Output is correct |
16 |
Correct |
25 ms |
23800 KB |
Output is correct |
17 |
Correct |
24 ms |
23928 KB |
Output is correct |
18 |
Correct |
43 ms |
23924 KB |
Output is correct |
19 |
Correct |
24 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
23928 KB |
Output is correct |
21 |
Correct |
24 ms |
23928 KB |
Output is correct |
22 |
Correct |
54 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
24 ms |
23800 KB |
Output is correct |
4 |
Correct |
24 ms |
23772 KB |
Output is correct |
5 |
Correct |
25 ms |
23800 KB |
Output is correct |
6 |
Correct |
24 ms |
23800 KB |
Output is correct |
7 |
Correct |
24 ms |
23800 KB |
Output is correct |
8 |
Correct |
24 ms |
23800 KB |
Output is correct |
9 |
Correct |
26 ms |
23800 KB |
Output is correct |
10 |
Correct |
26 ms |
23800 KB |
Output is correct |
11 |
Correct |
495 ms |
55288 KB |
Output is correct |
12 |
Correct |
471 ms |
52108 KB |
Output is correct |
13 |
Correct |
1159 ms |
56472 KB |
Output is correct |
14 |
Correct |
578 ms |
40212 KB |
Output is correct |
15 |
Correct |
1167 ms |
56180 KB |
Output is correct |
16 |
Correct |
1148 ms |
56388 KB |
Output is correct |
17 |
Correct |
26 ms |
23800 KB |
Output is correct |
18 |
Correct |
24 ms |
23928 KB |
Output is correct |
19 |
Correct |
26 ms |
23904 KB |
Output is correct |
20 |
Correct |
28 ms |
24056 KB |
Output is correct |
21 |
Correct |
25 ms |
24056 KB |
Output is correct |
22 |
Correct |
25 ms |
23800 KB |
Output is correct |
23 |
Correct |
24 ms |
23928 KB |
Output is correct |
24 |
Correct |
43 ms |
23924 KB |
Output is correct |
25 |
Correct |
24 ms |
23800 KB |
Output is correct |
26 |
Correct |
23 ms |
23928 KB |
Output is correct |
27 |
Correct |
24 ms |
23928 KB |
Output is correct |
28 |
Correct |
54 ms |
23800 KB |
Output is correct |
29 |
Correct |
23 ms |
23804 KB |
Output is correct |
30 |
Correct |
501 ms |
68572 KB |
Output is correct |
31 |
Correct |
494 ms |
68732 KB |
Output is correct |
32 |
Correct |
656 ms |
219628 KB |
Output is correct |
33 |
Correct |
584 ms |
178296 KB |
Output is correct |
34 |
Correct |
1319 ms |
69496 KB |
Output is correct |
35 |
Correct |
1274 ms |
69560 KB |
Output is correct |
36 |
Correct |
1226 ms |
88172 KB |
Output is correct |
37 |
Correct |
1221 ms |
88304 KB |
Output is correct |
38 |
Correct |
986 ms |
90096 KB |
Output is correct |
39 |
Correct |
960 ms |
90092 KB |
Output is correct |
40 |
Correct |
1004 ms |
90220 KB |
Output is correct |