/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
const int LOG_N = 18;
int N;
vector <int> adj[N_MAX + 2];
int parent[N_MAX + 2];
int depth[N_MAX + 2];
int sub[N_MAX + 2];
void dfs (int u) {
sub[u] = 1;
for (int v : adj[u]) {
if (v != parent[u]) {
parent[v] = u;
depth[v] = depth[u] + 1;
dfs(v);
sub[u] += sub[v];
}
}
}
int root;
int anc[LOG_N][N_MAX + 2];
void precalc () {
for (int u = 1; u <= N; u++) {
anc[0][u] = parent[u];
}
for (int k = 1; k < LOG_N; k++) {
for (int u = 1; u <= N; u++) {
anc[k][u] = anc[k - 1][anc[k - 1][u]];
}
}
}
int ancestor (int u, int len) {
for (int k = 0; k < LOG_N; k++) {
if ((len >> k) & 1) {
u = anc[k][u];
}
}
return u;
}
int lca (int u, int v) {
if (depth[u] > depth[v]) {
u = ancestor(u, depth[u] - depth[v]);
} else if (depth[v] > depth[u]) {
v = ancestor(v, depth[v] - depth[u]);
}
if (u == v) {
return u;
}
for (int k = LOG_N - 1; k >= 0; k--) {
if (anc[k][u] != anc[k][v]) {
u = anc[k][u]; v = anc[k][v];
}
}
return parent[u];
}
int dist (int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
int order[N_MAX + 2];
int answer[N_MAX + 2];
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
for (int u = 1; u <= N; u++) {
bool centroid = (N - sub[u] <= N / 2);
for (int v : adj[u]) {
if (v != parent[u] && sub[v] > N / 2) {
centroid = false;
break;
}
}
if (centroid == true) {
root = u;
break;
}
}
parent[root] = 0;
dfs(root);
precalc();
iota(order + 1, order + N + 1, 1);
sort(order + 1, order + N + 1, [&] (const int &u, const int &v) {
return sub[u] > sub[v];
});
for (int k = N / 2, i = 1, x = root, y = root, diam = 0; k >= 1; k--) {
while (i + 1 <= N && sub[order[i + 1]] >= k) {
int u = order[++i];
int dist_ux = dist(u, x);
int dist_uy = dist(u, y);
if (dist_ux < dist_uy) {
swap(dist_ux, dist_uy);
swap(x, y);
}
if (dist_ux > diam) {
diam = dist_ux;
y = u;
}
}
answer[k * 2] = diam;
}
for (int i = 1; i <= N; i++) {
cout << ++answer[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22956 KB |
Output is correct |
5 |
Correct |
4 ms |
22896 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22956 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22884 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22976 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
4 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22956 KB |
Output is correct |
5 |
Correct |
4 ms |
22896 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22956 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22884 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22976 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
4 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
23132 KB |
Output is correct |
23 |
Correct |
6 ms |
23384 KB |
Output is correct |
24 |
Correct |
6 ms |
23132 KB |
Output is correct |
25 |
Correct |
6 ms |
23112 KB |
Output is correct |
26 |
Correct |
6 ms |
23132 KB |
Output is correct |
27 |
Correct |
6 ms |
23132 KB |
Output is correct |
28 |
Correct |
6 ms |
23132 KB |
Output is correct |
29 |
Correct |
7 ms |
23132 KB |
Output is correct |
30 |
Correct |
6 ms |
23132 KB |
Output is correct |
31 |
Correct |
6 ms |
23132 KB |
Output is correct |
32 |
Correct |
7 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
5 ms |
23048 KB |
Output is correct |
35 |
Correct |
5 ms |
23132 KB |
Output is correct |
36 |
Correct |
6 ms |
23132 KB |
Output is correct |
37 |
Correct |
5 ms |
23132 KB |
Output is correct |
38 |
Correct |
6 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
22876 KB |
Output is correct |
2 |
Correct |
4 ms |
22876 KB |
Output is correct |
3 |
Correct |
4 ms |
22876 KB |
Output is correct |
4 |
Correct |
5 ms |
22956 KB |
Output is correct |
5 |
Correct |
4 ms |
22896 KB |
Output is correct |
6 |
Correct |
4 ms |
22876 KB |
Output is correct |
7 |
Correct |
4 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
22876 KB |
Output is correct |
9 |
Correct |
4 ms |
22956 KB |
Output is correct |
10 |
Correct |
4 ms |
22876 KB |
Output is correct |
11 |
Correct |
4 ms |
22876 KB |
Output is correct |
12 |
Correct |
4 ms |
22876 KB |
Output is correct |
13 |
Correct |
4 ms |
22876 KB |
Output is correct |
14 |
Correct |
4 ms |
22876 KB |
Output is correct |
15 |
Correct |
4 ms |
22884 KB |
Output is correct |
16 |
Correct |
4 ms |
22876 KB |
Output is correct |
17 |
Correct |
4 ms |
22976 KB |
Output is correct |
18 |
Correct |
4 ms |
22876 KB |
Output is correct |
19 |
Correct |
4 ms |
22876 KB |
Output is correct |
20 |
Correct |
4 ms |
22876 KB |
Output is correct |
21 |
Correct |
5 ms |
22876 KB |
Output is correct |
22 |
Correct |
6 ms |
23132 KB |
Output is correct |
23 |
Correct |
6 ms |
23384 KB |
Output is correct |
24 |
Correct |
6 ms |
23132 KB |
Output is correct |
25 |
Correct |
6 ms |
23112 KB |
Output is correct |
26 |
Correct |
6 ms |
23132 KB |
Output is correct |
27 |
Correct |
6 ms |
23132 KB |
Output is correct |
28 |
Correct |
6 ms |
23132 KB |
Output is correct |
29 |
Correct |
7 ms |
23132 KB |
Output is correct |
30 |
Correct |
6 ms |
23132 KB |
Output is correct |
31 |
Correct |
6 ms |
23132 KB |
Output is correct |
32 |
Correct |
7 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
5 ms |
23048 KB |
Output is correct |
35 |
Correct |
5 ms |
23132 KB |
Output is correct |
36 |
Correct |
6 ms |
23132 KB |
Output is correct |
37 |
Correct |
5 ms |
23132 KB |
Output is correct |
38 |
Correct |
6 ms |
23132 KB |
Output is correct |
39 |
Correct |
138 ms |
32340 KB |
Output is correct |
40 |
Correct |
125 ms |
32072 KB |
Output is correct |
41 |
Correct |
130 ms |
32340 KB |
Output is correct |
42 |
Correct |
138 ms |
32352 KB |
Output is correct |
43 |
Correct |
136 ms |
32400 KB |
Output is correct |
44 |
Correct |
147 ms |
32596 KB |
Output is correct |
45 |
Correct |
211 ms |
37392 KB |
Output is correct |
46 |
Correct |
144 ms |
41300 KB |
Output is correct |
47 |
Correct |
105 ms |
32748 KB |
Output is correct |
48 |
Correct |
99 ms |
32968 KB |
Output is correct |
49 |
Correct |
164 ms |
33172 KB |
Output is correct |
50 |
Correct |
83 ms |
32968 KB |
Output is correct |
51 |
Correct |
141 ms |
40512 KB |
Output is correct |