#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> sz(n);
function<void(int, int)> DfsSz = [&](int v, int pv) {
sz[v] = 1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
DfsSz(u, v);
sz[v] += sz[u];
}
};
DfsSz(0, 0);
function<int(int, int)> Find = [&](int v, int pv) {
for (int u : g[v]) {
if (u == pv) {
continue;
}
if (sz[u] * 2 >= n) {
return Find(u, v);
}
}
return v;
};
int c = Find(0, 0);
const int L = 25;
vector<vector<int>> jump(n, vector<int>(L));
vector<int> dep(n);
vector<int> dfn(n);
vector<int> dfo(n);
int T = 0;
function<void(int, int)> Dfs = [&](int v, int pv) {
dfn[v] = ++T;
sz[v] = 1;
jump[v][0] = pv;
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
sz[v] += sz[u];
}
dfo[v] = T;
};
Dfs(c, c);
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
jump[i][j] = jump[jump[i][j - 1]][j - 1];
}
}
auto LCA = [&](int x, int y) {
if (dep[x] < dep[y]) {
swap(x, y);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[jump[x][i]] >= dep[y]) {
x = jump[x][i];
}
}
if (x == y) {
return x;
}
for (int i = L - 1; i >= 0; i--) {
if (jump[x][i] != jump[y][i]) {
x = jump[x][i];
y = jump[y][i];
}
}
return jump[x][0];
};
auto GetDist = [&](int x, int y) {
return dep[x] + dep[y] - 2 * dep[LCA(x, y)];
};
vector<vector<int>> ver(n + 1);
for (int i = 0; i < n; i++) {
ver[sz[i]].push_back(i);
}
int x = c;
int y = c;
int d = 0;
vector<int> ans(n + 1);
for (int i = n / 2; i >= 1; i--) {
for (int z : ver[i]) {
int d_xz = GetDist(x, z);
int d_yz = GetDist(y, z);
if (d_xz < d_yz) {
swap(x, y);
swap(d_xz, d_yz);
}
if (d_xz > d) {
d = d_xz;
y = z;
}
}
ans[2 * i] = d;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] + 1 << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1232 KB |
Output is correct |
26 |
Correct |
5 ms |
1228 KB |
Output is correct |
27 |
Correct |
5 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
5 ms |
1232 KB |
Output is correct |
32 |
Correct |
5 ms |
1504 KB |
Output is correct |
33 |
Correct |
5 ms |
1584 KB |
Output is correct |
34 |
Correct |
4 ms |
1236 KB |
Output is correct |
35 |
Correct |
4 ms |
1236 KB |
Output is correct |
36 |
Correct |
5 ms |
1236 KB |
Output is correct |
37 |
Correct |
4 ms |
1236 KB |
Output is correct |
38 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
5 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
5 ms |
1236 KB |
Output is correct |
25 |
Correct |
5 ms |
1232 KB |
Output is correct |
26 |
Correct |
5 ms |
1228 KB |
Output is correct |
27 |
Correct |
5 ms |
1236 KB |
Output is correct |
28 |
Correct |
5 ms |
1236 KB |
Output is correct |
29 |
Correct |
5 ms |
1236 KB |
Output is correct |
30 |
Correct |
5 ms |
1236 KB |
Output is correct |
31 |
Correct |
5 ms |
1232 KB |
Output is correct |
32 |
Correct |
5 ms |
1504 KB |
Output is correct |
33 |
Correct |
5 ms |
1584 KB |
Output is correct |
34 |
Correct |
4 ms |
1236 KB |
Output is correct |
35 |
Correct |
4 ms |
1236 KB |
Output is correct |
36 |
Correct |
5 ms |
1236 KB |
Output is correct |
37 |
Correct |
4 ms |
1236 KB |
Output is correct |
38 |
Correct |
5 ms |
1364 KB |
Output is correct |
39 |
Correct |
351 ms |
49628 KB |
Output is correct |
40 |
Correct |
355 ms |
48456 KB |
Output is correct |
41 |
Correct |
346 ms |
49936 KB |
Output is correct |
42 |
Correct |
342 ms |
50648 KB |
Output is correct |
43 |
Correct |
358 ms |
50628 KB |
Output is correct |
44 |
Correct |
363 ms |
50756 KB |
Output is correct |
45 |
Correct |
624 ms |
58268 KB |
Output is correct |
46 |
Correct |
457 ms |
63900 KB |
Output is correct |
47 |
Correct |
296 ms |
51136 KB |
Output is correct |
48 |
Correct |
265 ms |
51040 KB |
Output is correct |
49 |
Correct |
439 ms |
51420 KB |
Output is correct |
50 |
Correct |
262 ms |
51124 KB |
Output is correct |
51 |
Correct |
374 ms |
61036 KB |
Output is correct |