#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
int N, R, A, B;
vector<int> adj[300010];
int down[300010], par[300010];
int dep[300010];
int in[300010], out[300010];
int pv;
void dfs(int x, int p) {
down[x] = 0;
in[x] = ++ pv;
for (auto &y : adj[x]) {
if (y == p) continue;
par[y] = x;
dep[y] = dep[x] + 1;
dfs(y, x);
down[x] = max(down[x], down[y] + 1);
}
out[x] = pv;
}
int findmin(int x, int p, int dist) {
if (dist > B-1) return N + 1;
int ret = x;
for (auto &y : adj[x]) {
if (y == p) continue;
ret = min(ret, findmin(y, x, dist+1));
}
return ret;
}
int dp[300010];
int dp2[300010];
int simulate(int x, int p, int dist) {
if (dist > B) return N + 1;
int ret;
int d = 0;
for (auto &y : adj[x]) {
if (y == p || y == par[x]) continue;
d = max(d, down[y] + 1);
}
if (d < A) {
ret = x;
for (auto &y : adj[x]) {
if (y == p || y == par[x]) continue;
ret = min(ret, dp[y]);
}
}
else {
ret = 0;
for (auto &y : adj[x]) {
if (y == p || y == par[x]) continue;
if (down[y] >= A-1)
ret = max(ret, dp[y]);
}
}
for (auto &y : adj[x]) {
if (y == p) continue;
ret = min(ret, simulate(y, x, dist + 1));
}
return ret;
}
vector<int> vtx[300010];
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
cin >> N >> R >> A >> B;
if (A <= B) {
cout << "1\n";
return 0;
}
for (int i=0; i<N-1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(R, 0);
int mxdep = 0;
for (int i=1; i<=N; i++) {
mxdep = max(mxdep, dep[i]);
vtx[dep[i]].push_back(i);
}
for (int i=mxdep; i>=1; i--) {
if (i+A-1 <= mxdep) {
for (auto &x : vtx[i+A-1]) {
dp2[x] = simulate(x, -1, 0);
}
}
for (auto &x : vtx[i]) {
if (down[x] < A-1) {
dp[x] = min(par[x], findmin(x, par[x], 0));
}
else {
for (auto &y : vtx[i+A-1]) {
if (in[x] <= in[y] && in[y] <= out[x])
dp[x] = max(dp[x], dp2[y]);
}
assert(dp[x]);
}
}
}
if (down[R] < A) {
int ans = R;
for (auto &x : vtx[1]) {
ans = min(ans, dp[x]);
}
cout << ans << '\n';
}
else {
int ans = 0;
for (auto &x : vtx[1]) {
if (down[x] >= A-1)
ans = max(ans, dp[x]);
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14452 KB |
Output is correct |
3 |
Correct |
5 ms |
14544 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
291 ms |
64828 KB |
Output is correct |
2 |
Correct |
298 ms |
65108 KB |
Output is correct |
3 |
Correct |
2271 ms |
64832 KB |
Output is correct |
4 |
Execution timed out |
6062 ms |
62712 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14448 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14448 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14684 KB |
Output is correct |
15 |
Correct |
6 ms |
14684 KB |
Output is correct |
16 |
Correct |
6 ms |
14684 KB |
Output is correct |
17 |
Correct |
6 ms |
14684 KB |
Output is correct |
18 |
Correct |
6 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14680 KB |
Output is correct |
20 |
Correct |
6 ms |
14740 KB |
Output is correct |
21 |
Correct |
5 ms |
14724 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
17 ms |
14772 KB |
Output is correct |
24 |
Correct |
7 ms |
14680 KB |
Output is correct |
25 |
Correct |
30 ms |
14648 KB |
Output is correct |
26 |
Correct |
9 ms |
14684 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
5 ms |
14684 KB |
Output is correct |
29 |
Correct |
5 ms |
14804 KB |
Output is correct |
30 |
Correct |
28 ms |
14796 KB |
Output is correct |
31 |
Correct |
29 ms |
14680 KB |
Output is correct |
32 |
Correct |
28 ms |
14684 KB |
Output is correct |
33 |
Correct |
15 ms |
15100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
21104 KB |
Output is correct |
2 |
Correct |
479 ms |
21080 KB |
Output is correct |
3 |
Correct |
408 ms |
21080 KB |
Output is correct |
4 |
Correct |
326 ms |
21044 KB |
Output is correct |
5 |
Correct |
635 ms |
21088 KB |
Output is correct |
6 |
Correct |
521 ms |
21012 KB |
Output is correct |
7 |
Correct |
190 ms |
21072 KB |
Output is correct |
8 |
Correct |
37 ms |
20824 KB |
Output is correct |
9 |
Correct |
345 ms |
21072 KB |
Output is correct |
10 |
Correct |
181 ms |
20816 KB |
Output is correct |
11 |
Correct |
50 ms |
20612 KB |
Output is correct |
12 |
Correct |
32 ms |
20952 KB |
Output is correct |
13 |
Correct |
7 ms |
14428 KB |
Output is correct |
14 |
Correct |
5 ms |
14340 KB |
Output is correct |
15 |
Correct |
207 ms |
20384 KB |
Output is correct |
16 |
Execution timed out |
6051 ms |
21308 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14448 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
4 ms |
14428 KB |
Output is correct |
10 |
Correct |
5 ms |
14428 KB |
Output is correct |
11 |
Correct |
5 ms |
14428 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
5 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14684 KB |
Output is correct |
15 |
Correct |
6 ms |
14684 KB |
Output is correct |
16 |
Correct |
6 ms |
14684 KB |
Output is correct |
17 |
Correct |
6 ms |
14684 KB |
Output is correct |
18 |
Correct |
6 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14680 KB |
Output is correct |
20 |
Correct |
6 ms |
14740 KB |
Output is correct |
21 |
Correct |
5 ms |
14724 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
17 ms |
14772 KB |
Output is correct |
24 |
Correct |
7 ms |
14680 KB |
Output is correct |
25 |
Correct |
30 ms |
14648 KB |
Output is correct |
26 |
Correct |
9 ms |
14684 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
5 ms |
14684 KB |
Output is correct |
29 |
Correct |
5 ms |
14804 KB |
Output is correct |
30 |
Correct |
28 ms |
14796 KB |
Output is correct |
31 |
Correct |
29 ms |
14680 KB |
Output is correct |
32 |
Correct |
28 ms |
14684 KB |
Output is correct |
33 |
Correct |
15 ms |
15100 KB |
Output is correct |
34 |
Correct |
721 ms |
21116 KB |
Output is correct |
35 |
Correct |
541 ms |
21036 KB |
Output is correct |
36 |
Correct |
377 ms |
21076 KB |
Output is correct |
37 |
Correct |
386 ms |
20820 KB |
Output is correct |
38 |
Correct |
674 ms |
20944 KB |
Output is correct |
39 |
Correct |
493 ms |
20816 KB |
Output is correct |
40 |
Correct |
183 ms |
20888 KB |
Output is correct |
41 |
Correct |
40 ms |
20716 KB |
Output is correct |
42 |
Correct |
5 ms |
14428 KB |
Output is correct |
43 |
Correct |
3703 ms |
20344 KB |
Output is correct |
44 |
Execution timed out |
6038 ms |
21260 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14452 KB |
Output is correct |
3 |
Correct |
5 ms |
14544 KB |
Output is correct |
4 |
Correct |
5 ms |
14428 KB |
Output is correct |
5 |
Correct |
5 ms |
14428 KB |
Output is correct |
6 |
Correct |
291 ms |
64828 KB |
Output is correct |
7 |
Correct |
298 ms |
65108 KB |
Output is correct |
8 |
Correct |
2271 ms |
64832 KB |
Output is correct |
9 |
Execution timed out |
6062 ms |
62712 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |