#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN];
int sz[mxN];
int p[mxN][20];
int d[mxN];
int n;
void dfs(int u, int par) {
sz[u] = 1;
for (int i = 1; i < 20; i++) p[u][i] = p[p[u][i - 1]][i - 1];
for (int v : ad[u]) {
if (v == par) continue;
p[v][0] = u;
d[v] = d[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
}
int get(int u, int par) {
for (int v : ad[u]) {
if (v == par) continue;
if (sz[v] * 2 > n) return get(v, u);
}
return u;
}
int lca(int u, int v) {
if (d[u] < d[v]) swap(u, v);
for (int i = 19; i >= 0; i--) {
if (d[p[u][i]] >= d[v]) u = p[u][i];
}
if (u == v) return u;
for (int i = 19; i >= 0; i--) {
if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
}
return p[u][0];
}
int dist(int a, int b) {
return d[a] + d[b] - 2 * d[lca(a, b)];
}
void testCase() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
dfs(1, -1);
int c = get(1, -1);
p[c][0] = c;
d[c] = 0;
dfs(c, -1);
vector<int> ans(n + 1);
vector<int> v[n + 1];
for (int i = 1; i <= n; i++) {
v[sz[i]].push_back(i);
}
int a = c, b = c, d = 0;
for (int i = n; i >= 1; i--) {
for (int o : v[i]) {
int ao = dist(a, o);
int bo = dist(b, o);
if (max(ao, bo) > d) {
if (ao > bo) {
d = ao;
b = o;
} else {
d = bo;
a = o;
}
}
}
ans[i] = max(ans[i], d + 1);
ans[i - 1] = max(ans[i - 1], ans[i]);
}
for (int i = 1; i <= n; i++) {
if (i & 1) cout << "1\n";
else cout << ans[i / 2] << "\n";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
5024 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
5024 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
7 ms |
5708 KB |
Output is correct |
23 |
Correct |
7 ms |
5648 KB |
Output is correct |
24 |
Correct |
7 ms |
5580 KB |
Output is correct |
25 |
Correct |
7 ms |
5668 KB |
Output is correct |
26 |
Correct |
7 ms |
5580 KB |
Output is correct |
27 |
Correct |
6 ms |
5664 KB |
Output is correct |
28 |
Correct |
6 ms |
5668 KB |
Output is correct |
29 |
Correct |
7 ms |
5580 KB |
Output is correct |
30 |
Correct |
6 ms |
5668 KB |
Output is correct |
31 |
Correct |
6 ms |
5664 KB |
Output is correct |
32 |
Correct |
7 ms |
5672 KB |
Output is correct |
33 |
Correct |
8 ms |
5788 KB |
Output is correct |
34 |
Correct |
6 ms |
5584 KB |
Output is correct |
35 |
Correct |
8 ms |
5592 KB |
Output is correct |
36 |
Correct |
6 ms |
5580 KB |
Output is correct |
37 |
Correct |
6 ms |
5708 KB |
Output is correct |
38 |
Correct |
8 ms |
5708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5016 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
3 ms |
5028 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
5024 KB |
Output is correct |
17 |
Correct |
3 ms |
5016 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
7 ms |
5708 KB |
Output is correct |
23 |
Correct |
7 ms |
5648 KB |
Output is correct |
24 |
Correct |
7 ms |
5580 KB |
Output is correct |
25 |
Correct |
7 ms |
5668 KB |
Output is correct |
26 |
Correct |
7 ms |
5580 KB |
Output is correct |
27 |
Correct |
6 ms |
5664 KB |
Output is correct |
28 |
Correct |
6 ms |
5668 KB |
Output is correct |
29 |
Correct |
7 ms |
5580 KB |
Output is correct |
30 |
Correct |
6 ms |
5668 KB |
Output is correct |
31 |
Correct |
6 ms |
5664 KB |
Output is correct |
32 |
Correct |
7 ms |
5672 KB |
Output is correct |
33 |
Correct |
8 ms |
5788 KB |
Output is correct |
34 |
Correct |
6 ms |
5584 KB |
Output is correct |
35 |
Correct |
8 ms |
5592 KB |
Output is correct |
36 |
Correct |
6 ms |
5580 KB |
Output is correct |
37 |
Correct |
6 ms |
5708 KB |
Output is correct |
38 |
Correct |
8 ms |
5708 KB |
Output is correct |
39 |
Correct |
386 ms |
37500 KB |
Output is correct |
40 |
Correct |
299 ms |
36804 KB |
Output is correct |
41 |
Correct |
323 ms |
37544 KB |
Output is correct |
42 |
Correct |
322 ms |
38092 KB |
Output is correct |
43 |
Correct |
329 ms |
38104 KB |
Output is correct |
44 |
Correct |
324 ms |
38184 KB |
Output is correct |
45 |
Correct |
492 ms |
41012 KB |
Output is correct |
46 |
Correct |
385 ms |
46240 KB |
Output is correct |
47 |
Correct |
297 ms |
38500 KB |
Output is correct |
48 |
Correct |
213 ms |
38656 KB |
Output is correct |
49 |
Correct |
319 ms |
38844 KB |
Output is correct |
50 |
Correct |
234 ms |
38584 KB |
Output is correct |
51 |
Correct |
315 ms |
43964 KB |
Output is correct |