#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector
const int MXN = 4e5+10;
vec<int> tree[MXN];
int sz[MXN];
vec<pair<int, int>> sz_bst[MXN];
vec<int> ans(MXN, 1);
bool iscentr[MXN];
void compszs(int u, int p = -1) {
sz[u] = 0;
if(iscentr[u]) return;
sz[u] = 1;
for(int v : tree[u]) {
if(v == p) continue;
compszs(v, u);
sz[u] += sz[v];
}
}
int findcentr(int u, int mxsz, int p = -1) {
for(int v : tree[u]) {
if(v == p) continue;
if(sz[v] > mxsz) return findcentr(v, mxsz, u);
}
return u;
}
vec<pair<int, int>> upd(vec<pair<int, int>> bst, pair<int, int> val) {
if(bst.size() == 0) return {val};
if(val.second > bst[0].second) {
if(val.first == bst[0].first) {
bst[0] = val;
}
else {
if(bst.size() > 1) {
bst[1] = bst[0];
}
else {
bst.push_back(bst[0]);
}
bst[0] = val;
}
}
else if(bst.size() == 1) {
if(bst[0].first != val.first) {
bst.push_back(val);
}
}
else {
assert(bst.size() == 2);
if(val.second > bst[1].second && val.first != bst[0].first) {
bst[1] = val;
}
}
return bst;
}
void compbsts(int u, int p, int sa, int d) {
sz[u] = 0;
if(iscentr[u]) return;
sz[u] = 1;
for(int v : tree[u]) {
if(v == p) continue;
compbsts(v, u, sa, d+1);
sz[u] += sz[v];
}
sz_bst[sz[u]] = upd(sz_bst[sz[u]], {sa, d});
}
void divandconq(int u) {
compszs(u);
int v = findcentr(u, sz[u]/2);
for(int i = 0; i<=sz[u]; i++) {
sz_bst[i] = {};
}
sz[v] = 0;
for(int w : tree[v]) {
compbsts(w, v, w, 1);
sz[v] += sz[w];
}
iscentr[v] = true;
int mxd = 0;
for(int i = sz[v]; i>=0; i--) {
if(sz_bst[i].size() > 0) {
mxd = max(sz_bst[i][0].second, mxd);
}
//cerr << mxd << ' ';
ans[i*2] = max(ans[i*2], mxd+1);
}
//cerr << '\n';
vec<pair<int, int>> bst{};
for(int i = sz[v]; i>=0; i--) {
for(auto sb : sz_bst[i]) {
bst = upd(bst, sb);
}
if(bst.size() > 1) {
ans[i*2] = max(ans[i*2], bst[0].second+bst[1].second+1);
}
}
for(int w : tree[v]) {
if(iscentr[w]) continue;
divandconq(w);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
for(int i = 0; i<N-1; i++) {
int U, V;
cin >> U >> V;
tree[U].push_back(V);
tree[V].push_back(U);
}
divandconq(1);
for(int i = 1; i<=N; i++) {
cout << ans[i] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23640 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
5 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23640 KB |
Output is correct |
7 |
Correct |
4 ms |
23472 KB |
Output is correct |
8 |
Correct |
5 ms |
23644 KB |
Output is correct |
9 |
Correct |
5 ms |
23576 KB |
Output is correct |
10 |
Correct |
5 ms |
23624 KB |
Output is correct |
11 |
Correct |
5 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
4 ms |
23580 KB |
Output is correct |
14 |
Correct |
5 ms |
23640 KB |
Output is correct |
15 |
Correct |
4 ms |
23644 KB |
Output is correct |
16 |
Correct |
7 ms |
23640 KB |
Output is correct |
17 |
Correct |
4 ms |
23640 KB |
Output is correct |
18 |
Correct |
5 ms |
23644 KB |
Output is correct |
19 |
Correct |
5 ms |
23640 KB |
Output is correct |
20 |
Correct |
5 ms |
23644 KB |
Output is correct |
21 |
Correct |
7 ms |
23520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23640 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
5 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23640 KB |
Output is correct |
7 |
Correct |
4 ms |
23472 KB |
Output is correct |
8 |
Correct |
5 ms |
23644 KB |
Output is correct |
9 |
Correct |
5 ms |
23576 KB |
Output is correct |
10 |
Correct |
5 ms |
23624 KB |
Output is correct |
11 |
Correct |
5 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
4 ms |
23580 KB |
Output is correct |
14 |
Correct |
5 ms |
23640 KB |
Output is correct |
15 |
Correct |
4 ms |
23644 KB |
Output is correct |
16 |
Correct |
7 ms |
23640 KB |
Output is correct |
17 |
Correct |
4 ms |
23640 KB |
Output is correct |
18 |
Correct |
5 ms |
23644 KB |
Output is correct |
19 |
Correct |
5 ms |
23640 KB |
Output is correct |
20 |
Correct |
5 ms |
23644 KB |
Output is correct |
21 |
Correct |
7 ms |
23520 KB |
Output is correct |
22 |
Correct |
7 ms |
23644 KB |
Output is correct |
23 |
Correct |
7 ms |
23796 KB |
Output is correct |
24 |
Correct |
9 ms |
23644 KB |
Output is correct |
25 |
Correct |
7 ms |
23644 KB |
Output is correct |
26 |
Correct |
6 ms |
23644 KB |
Output is correct |
27 |
Correct |
7 ms |
23644 KB |
Output is correct |
28 |
Correct |
7 ms |
23640 KB |
Output is correct |
29 |
Correct |
7 ms |
23644 KB |
Output is correct |
30 |
Correct |
7 ms |
23644 KB |
Output is correct |
31 |
Correct |
7 ms |
23644 KB |
Output is correct |
32 |
Correct |
8 ms |
23896 KB |
Output is correct |
33 |
Correct |
10 ms |
24216 KB |
Output is correct |
34 |
Correct |
5 ms |
23644 KB |
Output is correct |
35 |
Correct |
6 ms |
23640 KB |
Output is correct |
36 |
Correct |
7 ms |
23900 KB |
Output is correct |
37 |
Correct |
8 ms |
23960 KB |
Output is correct |
38 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
23640 KB |
Output is correct |
2 |
Correct |
5 ms |
23644 KB |
Output is correct |
3 |
Correct |
5 ms |
23644 KB |
Output is correct |
4 |
Correct |
6 ms |
23644 KB |
Output is correct |
5 |
Correct |
5 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23640 KB |
Output is correct |
7 |
Correct |
4 ms |
23472 KB |
Output is correct |
8 |
Correct |
5 ms |
23644 KB |
Output is correct |
9 |
Correct |
5 ms |
23576 KB |
Output is correct |
10 |
Correct |
5 ms |
23624 KB |
Output is correct |
11 |
Correct |
5 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
4 ms |
23580 KB |
Output is correct |
14 |
Correct |
5 ms |
23640 KB |
Output is correct |
15 |
Correct |
4 ms |
23644 KB |
Output is correct |
16 |
Correct |
7 ms |
23640 KB |
Output is correct |
17 |
Correct |
4 ms |
23640 KB |
Output is correct |
18 |
Correct |
5 ms |
23644 KB |
Output is correct |
19 |
Correct |
5 ms |
23640 KB |
Output is correct |
20 |
Correct |
5 ms |
23644 KB |
Output is correct |
21 |
Correct |
7 ms |
23520 KB |
Output is correct |
22 |
Correct |
7 ms |
23644 KB |
Output is correct |
23 |
Correct |
7 ms |
23796 KB |
Output is correct |
24 |
Correct |
9 ms |
23644 KB |
Output is correct |
25 |
Correct |
7 ms |
23644 KB |
Output is correct |
26 |
Correct |
6 ms |
23644 KB |
Output is correct |
27 |
Correct |
7 ms |
23644 KB |
Output is correct |
28 |
Correct |
7 ms |
23640 KB |
Output is correct |
29 |
Correct |
7 ms |
23644 KB |
Output is correct |
30 |
Correct |
7 ms |
23644 KB |
Output is correct |
31 |
Correct |
7 ms |
23644 KB |
Output is correct |
32 |
Correct |
8 ms |
23896 KB |
Output is correct |
33 |
Correct |
10 ms |
24216 KB |
Output is correct |
34 |
Correct |
5 ms |
23644 KB |
Output is correct |
35 |
Correct |
6 ms |
23640 KB |
Output is correct |
36 |
Correct |
7 ms |
23900 KB |
Output is correct |
37 |
Correct |
8 ms |
23960 KB |
Output is correct |
38 |
Correct |
10 ms |
23900 KB |
Output is correct |
39 |
Correct |
208 ms |
34868 KB |
Output is correct |
40 |
Correct |
220 ms |
35840 KB |
Output is correct |
41 |
Correct |
231 ms |
35156 KB |
Output is correct |
42 |
Correct |
186 ms |
36180 KB |
Output is correct |
43 |
Correct |
195 ms |
35156 KB |
Output is correct |
44 |
Correct |
208 ms |
36124 KB |
Output is correct |
45 |
Correct |
402 ms |
47500 KB |
Output is correct |
46 |
Correct |
403 ms |
55908 KB |
Output is correct |
47 |
Correct |
81 ms |
36296 KB |
Output is correct |
48 |
Correct |
68 ms |
36676 KB |
Output is correct |
49 |
Correct |
207 ms |
36392 KB |
Output is correct |
50 |
Correct |
94 ms |
36540 KB |
Output is correct |
51 |
Correct |
225 ms |
46036 KB |
Output is correct |