#include "bits/stdc++.h"
using namespace std;
#define int long long
const int N = 2e5 + 10;
const int LG = 18;
int n, sz[N], a, b, ans[N], dep[N], lift[N][LG];
vector<int> adj[N], node[N];
int dfs(int u, int p) {
sz[u] = 1;
int mx = 0, res = -1;
for (int v : adj[u]) {
if (v == p) continue;
lift[v][0] = u;
res = max(res, dfs(v, u));
sz[u] += sz[v];
mx = max(mx, sz[v]);
}
mx = max(mx, n - sz[u]);
if (mx <= n / 2) return u;
return res;
}
void comp(int u, int p) {
for (int i = 1; i < LG; i++) lift[u][i] = lift[lift[u][i-1]][i-1];
sz[u] = 1;
for (int v : adj[u]) {
if (v == p) continue;
lift[v][0] = u;
dep[v] = dep[u] + 1;
comp(v, u);
sz[u] += sz[v];
}
}
int jump(int sta, int di) {
for (int i = LG - 1; i >= 0; i--) if (di & (1 << i)) sta = lift[sta][i];
return sta;
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
x = jump(x, dep[x] - dep[y]);
if (x == y) return x;
for (int i = LG - 1; i >= 0; i--) {
int xt = lift[x][i], yt = lift[y][i];
if (xt != yt) x = xt, y = yt;
}
return lift[x][0];
}
int dist(int u, int v) {
return dep[u] + dep[v] - dep[lca(u, v)] * 2;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int c = dfs(1, -1);
dfs(c, -1);
lift[c][0] = c;
comp(c, -1);
for (int i = 1; i <= n; i++) node[sz[i]].push_back(i);
for (int i = n; i >= 1; i--) {
if (i % 2) continue;
ans[i] = ans[i + 2];
for (auto& x : node[i/2]) {
ans[i] = max(ans[i], dep[x]);
if (!a) a = b = x;
}
for (auto& x : node[i/2]) {
int dax = dist(a, x);
int dbx = dist(b, x);
int dab = dist(a, b);
if (dax >= dab && dax >= dbx) b = x;
else if (dbx >= dab && dbx >= dax) a = x;
}
ans[i] = max(ans[i], dist(a, b));
}
for (int i = 1; i <= n; i++) cout << ans[i] + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9808 KB |
Output is correct |
2 |
Correct |
8 ms |
9808 KB |
Output is correct |
3 |
Correct |
9 ms |
10064 KB |
Output is correct |
4 |
Correct |
8 ms |
9808 KB |
Output is correct |
5 |
Correct |
8 ms |
9808 KB |
Output is correct |
6 |
Correct |
8 ms |
9808 KB |
Output is correct |
7 |
Correct |
9 ms |
9808 KB |
Output is correct |
8 |
Correct |
10 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9808 KB |
Output is correct |
10 |
Correct |
8 ms |
9808 KB |
Output is correct |
11 |
Correct |
10 ms |
9844 KB |
Output is correct |
12 |
Correct |
9 ms |
9808 KB |
Output is correct |
13 |
Correct |
7 ms |
9808 KB |
Output is correct |
14 |
Correct |
6 ms |
9872 KB |
Output is correct |
15 |
Correct |
7 ms |
9824 KB |
Output is correct |
16 |
Correct |
7 ms |
9876 KB |
Output is correct |
17 |
Correct |
7 ms |
9648 KB |
Output is correct |
18 |
Correct |
7 ms |
9976 KB |
Output is correct |
19 |
Correct |
7 ms |
9808 KB |
Output is correct |
20 |
Correct |
9 ms |
9808 KB |
Output is correct |
21 |
Correct |
8 ms |
9808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9808 KB |
Output is correct |
2 |
Correct |
8 ms |
9808 KB |
Output is correct |
3 |
Correct |
9 ms |
10064 KB |
Output is correct |
4 |
Correct |
8 ms |
9808 KB |
Output is correct |
5 |
Correct |
8 ms |
9808 KB |
Output is correct |
6 |
Correct |
8 ms |
9808 KB |
Output is correct |
7 |
Correct |
9 ms |
9808 KB |
Output is correct |
8 |
Correct |
10 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9808 KB |
Output is correct |
10 |
Correct |
8 ms |
9808 KB |
Output is correct |
11 |
Correct |
10 ms |
9844 KB |
Output is correct |
12 |
Correct |
9 ms |
9808 KB |
Output is correct |
13 |
Correct |
7 ms |
9808 KB |
Output is correct |
14 |
Correct |
6 ms |
9872 KB |
Output is correct |
15 |
Correct |
7 ms |
9824 KB |
Output is correct |
16 |
Correct |
7 ms |
9876 KB |
Output is correct |
17 |
Correct |
7 ms |
9648 KB |
Output is correct |
18 |
Correct |
7 ms |
9976 KB |
Output is correct |
19 |
Correct |
7 ms |
9808 KB |
Output is correct |
20 |
Correct |
9 ms |
9808 KB |
Output is correct |
21 |
Correct |
8 ms |
9808 KB |
Output is correct |
22 |
Correct |
9 ms |
10576 KB |
Output is correct |
23 |
Correct |
9 ms |
10576 KB |
Output is correct |
24 |
Correct |
9 ms |
10576 KB |
Output is correct |
25 |
Correct |
9 ms |
10576 KB |
Output is correct |
26 |
Correct |
9 ms |
10576 KB |
Output is correct |
27 |
Correct |
9 ms |
10648 KB |
Output is correct |
28 |
Correct |
9 ms |
10576 KB |
Output is correct |
29 |
Correct |
13 ms |
10772 KB |
Output is correct |
30 |
Correct |
9 ms |
10576 KB |
Output is correct |
31 |
Correct |
9 ms |
10576 KB |
Output is correct |
32 |
Correct |
10 ms |
10832 KB |
Output is correct |
33 |
Correct |
9 ms |
10832 KB |
Output is correct |
34 |
Correct |
9 ms |
10744 KB |
Output is correct |
35 |
Correct |
10 ms |
10832 KB |
Output is correct |
36 |
Correct |
9 ms |
10576 KB |
Output is correct |
37 |
Correct |
9 ms |
10576 KB |
Output is correct |
38 |
Correct |
9 ms |
10832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9808 KB |
Output is correct |
2 |
Correct |
8 ms |
9808 KB |
Output is correct |
3 |
Correct |
9 ms |
10064 KB |
Output is correct |
4 |
Correct |
8 ms |
9808 KB |
Output is correct |
5 |
Correct |
8 ms |
9808 KB |
Output is correct |
6 |
Correct |
8 ms |
9808 KB |
Output is correct |
7 |
Correct |
9 ms |
9808 KB |
Output is correct |
8 |
Correct |
10 ms |
9808 KB |
Output is correct |
9 |
Correct |
7 ms |
9808 KB |
Output is correct |
10 |
Correct |
8 ms |
9808 KB |
Output is correct |
11 |
Correct |
10 ms |
9844 KB |
Output is correct |
12 |
Correct |
9 ms |
9808 KB |
Output is correct |
13 |
Correct |
7 ms |
9808 KB |
Output is correct |
14 |
Correct |
6 ms |
9872 KB |
Output is correct |
15 |
Correct |
7 ms |
9824 KB |
Output is correct |
16 |
Correct |
7 ms |
9876 KB |
Output is correct |
17 |
Correct |
7 ms |
9648 KB |
Output is correct |
18 |
Correct |
7 ms |
9976 KB |
Output is correct |
19 |
Correct |
7 ms |
9808 KB |
Output is correct |
20 |
Correct |
9 ms |
9808 KB |
Output is correct |
21 |
Correct |
8 ms |
9808 KB |
Output is correct |
22 |
Correct |
9 ms |
10576 KB |
Output is correct |
23 |
Correct |
9 ms |
10576 KB |
Output is correct |
24 |
Correct |
9 ms |
10576 KB |
Output is correct |
25 |
Correct |
9 ms |
10576 KB |
Output is correct |
26 |
Correct |
9 ms |
10576 KB |
Output is correct |
27 |
Correct |
9 ms |
10648 KB |
Output is correct |
28 |
Correct |
9 ms |
10576 KB |
Output is correct |
29 |
Correct |
13 ms |
10772 KB |
Output is correct |
30 |
Correct |
9 ms |
10576 KB |
Output is correct |
31 |
Correct |
9 ms |
10576 KB |
Output is correct |
32 |
Correct |
10 ms |
10832 KB |
Output is correct |
33 |
Correct |
9 ms |
10832 KB |
Output is correct |
34 |
Correct |
9 ms |
10744 KB |
Output is correct |
35 |
Correct |
10 ms |
10832 KB |
Output is correct |
36 |
Correct |
9 ms |
10576 KB |
Output is correct |
37 |
Correct |
9 ms |
10576 KB |
Output is correct |
38 |
Correct |
9 ms |
10832 KB |
Output is correct |
39 |
Correct |
221 ms |
54064 KB |
Output is correct |
40 |
Correct |
182 ms |
52924 KB |
Output is correct |
41 |
Correct |
222 ms |
54464 KB |
Output is correct |
42 |
Correct |
260 ms |
54996 KB |
Output is correct |
43 |
Correct |
234 ms |
54972 KB |
Output is correct |
44 |
Correct |
247 ms |
55228 KB |
Output is correct |
45 |
Correct |
428 ms |
60812 KB |
Output is correct |
46 |
Correct |
296 ms |
64072 KB |
Output is correct |
47 |
Correct |
189 ms |
55128 KB |
Output is correct |
48 |
Correct |
156 ms |
54932 KB |
Output is correct |
49 |
Correct |
219 ms |
55488 KB |
Output is correct |
50 |
Correct |
159 ms |
55100 KB |
Output is correct |
51 |
Correct |
194 ms |
62908 KB |
Output is correct |