#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;
}
else {
int ans = 0;
for (auto &x : vtx[1]) {
if (down[x] >= A-1)
ans = max(ans, dp[x]);
}
cout << ans;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 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 |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
291 ms |
67412 KB |
Output is correct |
2 |
Correct |
289 ms |
67408 KB |
Output is correct |
3 |
Correct |
2282 ms |
67368 KB |
Output is correct |
4 |
Execution timed out |
6008 ms |
65064 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 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 |
14616 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 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 |
14520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 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 |
14616 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 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 |
14520 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14772 KB |
Output is correct |
16 |
Correct |
6 ms |
14684 KB |
Output is correct |
17 |
Correct |
8 ms |
14680 KB |
Output is correct |
18 |
Correct |
6 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14684 KB |
Output is correct |
20 |
Correct |
7 ms |
14684 KB |
Output is correct |
21 |
Correct |
6 ms |
14704 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
18 ms |
14792 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
30 ms |
14828 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 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 |
14684 KB |
Output is correct |
30 |
Correct |
29 ms |
14812 KB |
Output is correct |
31 |
Correct |
34 ms |
14684 KB |
Output is correct |
32 |
Correct |
31 ms |
14684 KB |
Output is correct |
33 |
Correct |
16 ms |
15120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
662 ms |
22100 KB |
Output is correct |
2 |
Correct |
483 ms |
21844 KB |
Output is correct |
3 |
Correct |
378 ms |
21848 KB |
Output is correct |
4 |
Correct |
314 ms |
21592 KB |
Output is correct |
5 |
Correct |
604 ms |
21848 KB |
Output is correct |
6 |
Correct |
513 ms |
21780 KB |
Output is correct |
7 |
Correct |
181 ms |
21836 KB |
Output is correct |
8 |
Correct |
39 ms |
21584 KB |
Output is correct |
9 |
Correct |
409 ms |
21528 KB |
Output is correct |
10 |
Correct |
185 ms |
20948 KB |
Output is correct |
11 |
Correct |
54 ms |
20560 KB |
Output is correct |
12 |
Correct |
39 ms |
20976 KB |
Output is correct |
13 |
Correct |
6 ms |
14424 KB |
Output is correct |
14 |
Correct |
7 ms |
14428 KB |
Output is correct |
15 |
Correct |
241 ms |
20388 KB |
Output is correct |
16 |
Execution timed out |
6024 ms |
21356 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 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 |
14616 KB |
Output is correct |
6 |
Correct |
5 ms |
14424 KB |
Output is correct |
7 |
Correct |
5 ms |
14428 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
5 ms |
14420 KB |
Output is correct |
10 |
Correct |
7 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 |
14520 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14772 KB |
Output is correct |
16 |
Correct |
6 ms |
14684 KB |
Output is correct |
17 |
Correct |
8 ms |
14680 KB |
Output is correct |
18 |
Correct |
6 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14684 KB |
Output is correct |
20 |
Correct |
7 ms |
14684 KB |
Output is correct |
21 |
Correct |
6 ms |
14704 KB |
Output is correct |
22 |
Correct |
5 ms |
14428 KB |
Output is correct |
23 |
Correct |
18 ms |
14792 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
30 ms |
14828 KB |
Output is correct |
26 |
Correct |
8 ms |
14680 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 |
14684 KB |
Output is correct |
30 |
Correct |
29 ms |
14812 KB |
Output is correct |
31 |
Correct |
34 ms |
14684 KB |
Output is correct |
32 |
Correct |
31 ms |
14684 KB |
Output is correct |
33 |
Correct |
16 ms |
15120 KB |
Output is correct |
34 |
Correct |
720 ms |
21084 KB |
Output is correct |
35 |
Correct |
573 ms |
20828 KB |
Output is correct |
36 |
Correct |
365 ms |
21304 KB |
Output is correct |
37 |
Correct |
408 ms |
21068 KB |
Output is correct |
38 |
Correct |
678 ms |
21080 KB |
Output is correct |
39 |
Correct |
528 ms |
20828 KB |
Output is correct |
40 |
Correct |
184 ms |
21084 KB |
Output is correct |
41 |
Correct |
34 ms |
20828 KB |
Output is correct |
42 |
Correct |
6 ms |
14428 KB |
Output is correct |
43 |
Correct |
3793 ms |
20336 KB |
Output is correct |
44 |
Execution timed out |
6063 ms |
21268 KB |
Time limit exceeded |
45 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 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 |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
291 ms |
67412 KB |
Output is correct |
7 |
Correct |
289 ms |
67408 KB |
Output is correct |
8 |
Correct |
2282 ms |
67368 KB |
Output is correct |
9 |
Execution timed out |
6008 ms |
65064 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |