#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9+9;
const ll LOGN = 20;
const ll MAXN = 2e5 + 100;
vector<vector<int>> graph;
vector<int> v[MAXN];
int sz[MAXN], depth[MAXN];
int up[LOGN][MAXN], ans[MAXN];
int get_sz(int node, int parent) {
sz[node] = 1;
for (auto u : graph[node]) {
if (u == parent)
continue;
sz[node] += get_sz(u, node);
}
return sz[node];
}
int find_centro(int node, int parent, int n) {
for (auto u : graph[node]) {
if (u != parent && sz[u] * 2 >= n)
return find_centro(u, node, n);
}
return node;
}
int dfs(int node, int parent) {
sz[node] = 1;
for (auto u : graph[node]) {
if (u == parent)
continue;
depth[u] = depth[node] + 1;
up[0][u] = node;
for (int i = 1; i < LOGN; i++)
up[i][u] = up[i-1][up[i-1][u]];
sz[node] += dfs(u, node);
}
v[sz[node]].push_back(node);
return sz[node];
}
int find(int node, int k) {
for (int i = 0; i < LOGN; i++) {
if ((1<<i) & k)
node = up[i][node];
}
return node;
}
int lca(int a, int b) {
if (depth[b] > depth[a])
swap(a, b);
a = find(a, depth[a] - depth[b]);
if (a == b)
return a;
for (int i = LOGN-1; i >= 0; i--) {
if (up[i][a] != up[i][b]) {
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
}
int dist(int a, int b) {
return depth[a] + depth[b] - 2*depth[lca(a, b)];
}
int main() {
fast
int N, A, B;
cin >> N;
graph = vector<vector<int>>(N+1, vector<int>());
for (int i = 1; i < N; i++) {
cin >> A >> B;
graph[A].push_back(B);
graph[B].push_back(A);
}
get_sz(1, 1);
int centro = find_centro(1, 1, sz[1]);
dfs(centro, centro);
for (int i = 0; i < MAXN; i++)
ans[i] = 1;
int n1 = centro, n2 = centro, diameter = 0;
for (int i = N; i > N / 2; i--) {
for (auto u : v[i]) {
if (dist(u, n1) > diameter)
n2 = u, diameter = dist(u, n1);
else if (dist(u, n2) > diameter)
n1 = u, diameter = dist(u, n2);
}
}
for (int i = N / 2; i >= 1; i--) {
for (auto u : v[i]) {
if (dist(u, n1) > diameter)
n2 = u, diameter = dist(u, n1);
else if (dist(u, n2) > diameter)
n1 = u, diameter = dist(u, n2);
}
ans[i*2] = diameter + 1;
}
for (int i = 1; i <= N; i++)
cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
4 ms |
21068 KB |
Output is correct |
3 |
Correct |
4 ms |
21084 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21084 KB |
Output is correct |
8 |
Correct |
4 ms |
20912 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21084 KB |
Output is correct |
15 |
Correct |
4 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
3 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
20908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
4 ms |
21068 KB |
Output is correct |
3 |
Correct |
4 ms |
21084 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21084 KB |
Output is correct |
8 |
Correct |
4 ms |
20912 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21084 KB |
Output is correct |
15 |
Correct |
4 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
3 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
20908 KB |
Output is correct |
22 |
Correct |
6 ms |
21336 KB |
Output is correct |
23 |
Correct |
6 ms |
21428 KB |
Output is correct |
24 |
Correct |
6 ms |
21340 KB |
Output is correct |
25 |
Correct |
6 ms |
21424 KB |
Output is correct |
26 |
Correct |
6 ms |
21340 KB |
Output is correct |
27 |
Correct |
6 ms |
21340 KB |
Output is correct |
28 |
Correct |
6 ms |
21360 KB |
Output is correct |
29 |
Correct |
6 ms |
21476 KB |
Output is correct |
30 |
Correct |
5 ms |
21336 KB |
Output is correct |
31 |
Correct |
6 ms |
21340 KB |
Output is correct |
32 |
Correct |
6 ms |
21596 KB |
Output is correct |
33 |
Correct |
6 ms |
21596 KB |
Output is correct |
34 |
Correct |
6 ms |
21476 KB |
Output is correct |
35 |
Correct |
5 ms |
21336 KB |
Output is correct |
36 |
Correct |
7 ms |
21336 KB |
Output is correct |
37 |
Correct |
5 ms |
21340 KB |
Output is correct |
38 |
Correct |
6 ms |
21488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
4 ms |
21068 KB |
Output is correct |
3 |
Correct |
4 ms |
21084 KB |
Output is correct |
4 |
Correct |
4 ms |
21084 KB |
Output is correct |
5 |
Correct |
4 ms |
21084 KB |
Output is correct |
6 |
Correct |
4 ms |
21084 KB |
Output is correct |
7 |
Correct |
4 ms |
21084 KB |
Output is correct |
8 |
Correct |
4 ms |
20912 KB |
Output is correct |
9 |
Correct |
4 ms |
21084 KB |
Output is correct |
10 |
Correct |
4 ms |
21080 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 KB |
Output is correct |
14 |
Correct |
4 ms |
21084 KB |
Output is correct |
15 |
Correct |
4 ms |
21080 KB |
Output is correct |
16 |
Correct |
4 ms |
21080 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
3 ms |
21084 KB |
Output is correct |
19 |
Correct |
3 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
20908 KB |
Output is correct |
22 |
Correct |
6 ms |
21336 KB |
Output is correct |
23 |
Correct |
6 ms |
21428 KB |
Output is correct |
24 |
Correct |
6 ms |
21340 KB |
Output is correct |
25 |
Correct |
6 ms |
21424 KB |
Output is correct |
26 |
Correct |
6 ms |
21340 KB |
Output is correct |
27 |
Correct |
6 ms |
21340 KB |
Output is correct |
28 |
Correct |
6 ms |
21360 KB |
Output is correct |
29 |
Correct |
6 ms |
21476 KB |
Output is correct |
30 |
Correct |
5 ms |
21336 KB |
Output is correct |
31 |
Correct |
6 ms |
21340 KB |
Output is correct |
32 |
Correct |
6 ms |
21596 KB |
Output is correct |
33 |
Correct |
6 ms |
21596 KB |
Output is correct |
34 |
Correct |
6 ms |
21476 KB |
Output is correct |
35 |
Correct |
5 ms |
21336 KB |
Output is correct |
36 |
Correct |
7 ms |
21336 KB |
Output is correct |
37 |
Correct |
5 ms |
21340 KB |
Output is correct |
38 |
Correct |
6 ms |
21488 KB |
Output is correct |
39 |
Correct |
192 ms |
37856 KB |
Output is correct |
40 |
Correct |
168 ms |
37320 KB |
Output is correct |
41 |
Correct |
206 ms |
37932 KB |
Output is correct |
42 |
Correct |
179 ms |
38060 KB |
Output is correct |
43 |
Correct |
200 ms |
38180 KB |
Output is correct |
44 |
Correct |
168 ms |
38144 KB |
Output is correct |
45 |
Correct |
245 ms |
43980 KB |
Output is correct |
46 |
Correct |
159 ms |
48420 KB |
Output is correct |
47 |
Correct |
155 ms |
38596 KB |
Output is correct |
48 |
Correct |
137 ms |
38700 KB |
Output is correct |
49 |
Correct |
171 ms |
38832 KB |
Output is correct |
50 |
Correct |
149 ms |
38608 KB |
Output is correct |
51 |
Correct |
145 ms |
45536 KB |
Output is correct |