#include <bits/stdc++.h>
using namespace std;
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> g(n + 1);
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
vector<int> sub(n + 1);
function<void(int, int)> subsz = [&](int u, int p) {
for(int to : g[u]) {
if(to == p) {
continue;
}
sub[to] = 1;
subsz(to, u);
sub[u] += sub[to];
}
};
sub[1] = 1;
subsz(1, 1);
function<int(int, int, int)> getcen = [&](int u, int p, int d) {
for(int to : g[u]) {
if(to == p) {
continue;
}
if(sub[to] > d / 2) {
return getcen(to, u, d);
}
}
return u;
};
int root = getcen(1, 1, n);
vector<vector<int>> up(20, vector<int>(n + 1));
vector<int> dep(n + 1);
function<void(int, int)> prep = [&](int u, int p) {
for(int to : g[u]) {
if(to == p) {
continue;
}
dep[to] = dep[u] + 1;
up[0][to] = u;
sub[to] = 1;
prep(to, u);
sub[u] += sub[to];
}
};
up[0][root] = root;
sub[root] = 1;
prep(root, root);
vector<vector<int>> gg(n / 2 + 1);
for(int i = 1; i <= n; i++) {
if(i == root) {
continue;
}
gg[sub[i]].emplace_back(i);
}
for(int i = 1; i < 20; i++) {
for(int j = 1; j <= n; j++) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
auto jump = [&](int u, int d) {
for(int i = 0; d; i++) {
if(d % 2) {
u = up[i][u];
}
d /= 2;
}
return u;
};
auto lca = [&](int a, int b) {
if(dep[a] > dep[b]) {
swap(a, b);
}
b = jump(b, dep[b] - dep[a]);
if(a == b) {
return a;
}
for(int i = 19; i >= 0; i--) {
if(up[i][a] != up[i][b]) {
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
};
auto d = [&](int a, int b) {
return dep[a] + dep[b] - 2 * dep[lca(a, b)];
};
int a = root, b = root, c = 0;
vector<int> ans(n / 2 + 1);
for(int i = n / 2; i >= 1; i--) {
for(int j : gg[i]) {
int one = d(a, j);
int two = d(b, j);
if(one > c) {
b = j;
c = one;
}
else if(two > c) {
a = j;
c = two;
}
}
ans[i] = c;
}
for(int i = 1; i <= n; i++) {
cout << (i % 2 ? 1 : ans[i / 2] + 1) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
992 KB |
Output is correct |
24 |
Correct |
3 ms |
980 KB |
Output is correct |
25 |
Correct |
3 ms |
980 KB |
Output is correct |
26 |
Correct |
4 ms |
980 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
972 KB |
Output is correct |
30 |
Correct |
3 ms |
980 KB |
Output is correct |
31 |
Correct |
3 ms |
980 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
3 ms |
992 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
992 KB |
Output is correct |
24 |
Correct |
3 ms |
980 KB |
Output is correct |
25 |
Correct |
3 ms |
980 KB |
Output is correct |
26 |
Correct |
4 ms |
980 KB |
Output is correct |
27 |
Correct |
3 ms |
980 KB |
Output is correct |
28 |
Correct |
3 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
972 KB |
Output is correct |
30 |
Correct |
3 ms |
980 KB |
Output is correct |
31 |
Correct |
3 ms |
980 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
4 ms |
980 KB |
Output is correct |
36 |
Correct |
3 ms |
992 KB |
Output is correct |
37 |
Correct |
3 ms |
980 KB |
Output is correct |
38 |
Correct |
3 ms |
1108 KB |
Output is correct |
39 |
Correct |
226 ms |
34984 KB |
Output is correct |
40 |
Correct |
241 ms |
34076 KB |
Output is correct |
41 |
Correct |
227 ms |
35044 KB |
Output is correct |
42 |
Correct |
215 ms |
35744 KB |
Output is correct |
43 |
Correct |
248 ms |
35604 KB |
Output is correct |
44 |
Correct |
212 ms |
35640 KB |
Output is correct |
45 |
Correct |
366 ms |
43012 KB |
Output is correct |
46 |
Correct |
284 ms |
48324 KB |
Output is correct |
47 |
Correct |
196 ms |
36000 KB |
Output is correct |
48 |
Correct |
149 ms |
36232 KB |
Output is correct |
49 |
Correct |
299 ms |
36284 KB |
Output is correct |
50 |
Correct |
171 ms |
36416 KB |
Output is correct |
51 |
Correct |
209 ms |
45884 KB |
Output is correct |