#include <bits/stdc++.h>
#define int long long
using namespace std;
struct Val {
int val, i;
};
const int N = 2e5 + 42, T = 18, INF = 1e18;
Val val[N];
vector<int> adj[N];
int n, root, id = 0, sz[N], ans[N], dist[N], deb[N], fin[N], bl[T][N];
int DFS(int i, int pre) {
int maxi = 0, sum = 1;
for(int j : adj[i])
if(j != pre) {
int val = DFS(j, i);
sum += val;
maxi = max(maxi, val);
}
maxi = max(maxi, n - sum);
if(maxi <= n/2)
root = i;
return sum;
}
int getSz(int i, int pre) {
sz[i] = 1;
deb[i] = id++;
bl[0][i] = pre;
dist[i] = dist[pre] + 1;
for(int j : adj[i])
if(j != pre)
sz[i] += getSz(j, i);
fin[i] = id++;
return sz[i];
}
inline bool ancester(int a, int b) {
return deb[a] <= deb[b] && fin[b] <= fin[a];
}
int LCA(int a, int b) {
if(dist[a] < dist[b])
swap(a, b);
if(ancester(b, a))
return b;
for(int i = T-1; i > -1; i--)
if(dist[bl[i][a]] >= dist[b])
a = bl[i][a];
if(a == b)
return a;
for(int i = T-1; i > -1; i--)
if(bl[i][a] != bl[i][b])
a = bl[i][a], b = bl[i][b];
return bl[0][a];
}
inline int distance(int a, int b) {
return dist[a] + dist[b] - 2 * dist[LCA(a, b)];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
a--, b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
DFS(0, 0);
getSz(root, root);
for(int i = 0; i < n; i++) {
val[i].i = i;
val[i].val = sz[i];
}
sort(val, val + n,
[](Val a, Val b) {
return a.val > b.val;
});
for(int i = 1; i < T; i++)
for(int j = 0; j < n; j++)
bl[i][j] = bl[i-1][bl[i-1][j]];
int diam1 = root, diam2 = root;
for(int i = 1; i < n; i++) {
int dist1 = distance(diam1, diam2),
dist2 = distance(diam1, val[i].i),
dist3 = distance(val[i].i, diam2),
newmax = max(max(dist1, dist2), dist3);
if(dist2 == newmax)
diam2 = val[i].i;
else if(dist3 == newmax)
diam1 = val[i].i;
ans[val[i].val] = newmax;
}
for(int i = N-2; i > -1; i--)
ans[i] = max(ans[i], ans[i+1]);
for(int i = 1; i <= n; i++)
if(i % 2 == 1)
cout << "1\n";
else
cout << ans[i/2] + 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6740 KB |
Output is correct |
2 |
Correct |
6 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6696 KB |
Output is correct |
4 |
Correct |
5 ms |
6740 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
6 ms |
6696 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6752 KB |
Output is correct |
10 |
Correct |
5 ms |
6740 KB |
Output is correct |
11 |
Correct |
6 ms |
6740 KB |
Output is correct |
12 |
Correct |
5 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6740 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
6 ms |
6696 KB |
Output is correct |
16 |
Correct |
6 ms |
6740 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
5 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
6 ms |
6740 KB |
Output is correct |
21 |
Correct |
5 ms |
6740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6740 KB |
Output is correct |
2 |
Correct |
6 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6696 KB |
Output is correct |
4 |
Correct |
5 ms |
6740 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
6 ms |
6696 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6752 KB |
Output is correct |
10 |
Correct |
5 ms |
6740 KB |
Output is correct |
11 |
Correct |
6 ms |
6740 KB |
Output is correct |
12 |
Correct |
5 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6740 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
6 ms |
6696 KB |
Output is correct |
16 |
Correct |
6 ms |
6740 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
5 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
6 ms |
6740 KB |
Output is correct |
21 |
Correct |
5 ms |
6740 KB |
Output is correct |
22 |
Correct |
9 ms |
7636 KB |
Output is correct |
23 |
Correct |
9 ms |
7636 KB |
Output is correct |
24 |
Correct |
11 ms |
7636 KB |
Output is correct |
25 |
Correct |
9 ms |
7608 KB |
Output is correct |
26 |
Correct |
9 ms |
7636 KB |
Output is correct |
27 |
Correct |
9 ms |
7624 KB |
Output is correct |
28 |
Correct |
9 ms |
7700 KB |
Output is correct |
29 |
Correct |
9 ms |
7668 KB |
Output is correct |
30 |
Correct |
10 ms |
7604 KB |
Output is correct |
31 |
Correct |
8 ms |
7636 KB |
Output is correct |
32 |
Correct |
11 ms |
7732 KB |
Output is correct |
33 |
Correct |
9 ms |
7832 KB |
Output is correct |
34 |
Correct |
9 ms |
7664 KB |
Output is correct |
35 |
Correct |
7 ms |
7636 KB |
Output is correct |
36 |
Correct |
9 ms |
7636 KB |
Output is correct |
37 |
Correct |
8 ms |
7664 KB |
Output is correct |
38 |
Correct |
9 ms |
7672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
6740 KB |
Output is correct |
2 |
Correct |
6 ms |
6740 KB |
Output is correct |
3 |
Correct |
4 ms |
6696 KB |
Output is correct |
4 |
Correct |
5 ms |
6740 KB |
Output is correct |
5 |
Correct |
5 ms |
6740 KB |
Output is correct |
6 |
Correct |
6 ms |
6696 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
5 ms |
6740 KB |
Output is correct |
9 |
Correct |
4 ms |
6752 KB |
Output is correct |
10 |
Correct |
5 ms |
6740 KB |
Output is correct |
11 |
Correct |
6 ms |
6740 KB |
Output is correct |
12 |
Correct |
5 ms |
6740 KB |
Output is correct |
13 |
Correct |
6 ms |
6740 KB |
Output is correct |
14 |
Correct |
5 ms |
6740 KB |
Output is correct |
15 |
Correct |
6 ms |
6696 KB |
Output is correct |
16 |
Correct |
6 ms |
6740 KB |
Output is correct |
17 |
Correct |
5 ms |
6740 KB |
Output is correct |
18 |
Correct |
5 ms |
6740 KB |
Output is correct |
19 |
Correct |
5 ms |
6740 KB |
Output is correct |
20 |
Correct |
6 ms |
6740 KB |
Output is correct |
21 |
Correct |
5 ms |
6740 KB |
Output is correct |
22 |
Correct |
9 ms |
7636 KB |
Output is correct |
23 |
Correct |
9 ms |
7636 KB |
Output is correct |
24 |
Correct |
11 ms |
7636 KB |
Output is correct |
25 |
Correct |
9 ms |
7608 KB |
Output is correct |
26 |
Correct |
9 ms |
7636 KB |
Output is correct |
27 |
Correct |
9 ms |
7624 KB |
Output is correct |
28 |
Correct |
9 ms |
7700 KB |
Output is correct |
29 |
Correct |
9 ms |
7668 KB |
Output is correct |
30 |
Correct |
10 ms |
7604 KB |
Output is correct |
31 |
Correct |
8 ms |
7636 KB |
Output is correct |
32 |
Correct |
11 ms |
7732 KB |
Output is correct |
33 |
Correct |
9 ms |
7832 KB |
Output is correct |
34 |
Correct |
9 ms |
7664 KB |
Output is correct |
35 |
Correct |
7 ms |
7636 KB |
Output is correct |
36 |
Correct |
9 ms |
7636 KB |
Output is correct |
37 |
Correct |
8 ms |
7664 KB |
Output is correct |
38 |
Correct |
9 ms |
7672 KB |
Output is correct |
39 |
Correct |
489 ms |
53740 KB |
Output is correct |
40 |
Correct |
473 ms |
52784 KB |
Output is correct |
41 |
Correct |
480 ms |
53796 KB |
Output is correct |
42 |
Correct |
465 ms |
54524 KB |
Output is correct |
43 |
Correct |
488 ms |
54528 KB |
Output is correct |
44 |
Correct |
463 ms |
54536 KB |
Output is correct |
45 |
Correct |
1002 ms |
58412 KB |
Output is correct |
46 |
Correct |
737 ms |
60712 KB |
Output is correct |
47 |
Correct |
331 ms |
54576 KB |
Output is correct |
48 |
Correct |
227 ms |
54820 KB |
Output is correct |
49 |
Correct |
704 ms |
54744 KB |
Output is correct |
50 |
Correct |
258 ms |
54912 KB |
Output is correct |
51 |
Correct |
494 ms |
60332 KB |
Output is correct |