#include <bits/stdc++.h>
#define N 200005
#define INF 1000000000
using namespace std;
int n, sz[N], dpt[N], tb[N][18];
vector<int> adj[N];
bool vs[N];
void size_DFS(int c, int p) {
sz[c] = 1;
for(int i = 1; i < 18; i ++) {
tb[c][i] = tb[tb[c][i-1]][i-1];
}
for(int i: adj[c]) {
if(i == p) continue;
dpt[i] = dpt[c]+1;
tb[i][0] = c;
size_DFS(i, c);
sz[c] += sz[i];
}
}
int jump(int a, int ds) {
for(int i = 0; i < 18; i ++) {
if(ds&1<<i) a = tb[a][i];
}
return a;
}
int LCA(int a, int b) {
if(dpt[a] != dpt[b]) {
if(dpt[a] < dpt[b]) swap(a,b);
a = jump(a, dpt[a]-dpt[b]);
}
if(a == b) return a;
for(int i = 17; i >= 0; i --) {
if(tb[a][i] != tb[b][i]) {
a = tb[a][i];
b = tb[b][i];
}
}
return tb[a][0];
}
int getSize(int c, int rt) {
int lca = LCA(c,rt);
if(lca != c) {
return sz[c];
} else {
assert(dpt[rt] > dpt[c]);
return sz[1]-sz[jump(rt,dpt[rt]-dpt[c]-1)];
}
}
int sub_sz[N];
void size_2_DFS(int c, int p) {
sub_sz[c] = 1;
for(int i: adj[c]) {
if(vs[i] || i == p) continue;
size_2_DFS(i, c);
sub_sz[c] += sub_sz[i];
}
}
int find_cent_DFS(int c, int p, int tree_sz) {
for(int i: adj[c]) {
if(i == p || vs[i] || sub_sz[i]*2<tree_sz) continue;
return find_cent_DFS(i, c, tree_sz);
}
return c;
}
struct Segtree {
vector<int> tree;
int len;
void init(int n) {
len = 1<<18;
tree = vector<int>(len*2,-INF);
}
void update(int a, int v) {
tree[a+len] = max(tree[a+len],v);
for(a = (a+len)>>1; a >= 1; a >>= 1) {
tree[a] = max(tree[a*2],tree[a*2+1]);
}
}
void erase(int a) {
tree[a+len] = -INF;
for(a = (a+len)>>1; a >= 1; a >>= 1) {
tree[a] = max(tree[a*2],tree[a*2+1]);
}
}
int query(int a, int b) {
int res = -INF;
for(a += len, b += len; a <= b; a >>= 1, b >>= 1) {
if(a&1) res = max(res, tree[a++]);
if(~b&1) res = max(res, tree[b--]);
}
return res;
}
Segtree() {}
} tree;
int ans[N];
void ans_DFS(int c, int dt, int p, int rt) {
for(int i: adj[c]) {
if(vs[i] || i == p) continue;
ans_DFS(i, dt+1, c, rt);
}
if(c == rt) return;
int csz = getSize(c, rt);
ans[csz] = max(ans[csz], dt+tree.query(csz,tree.len-1)+1);
int rsz = getSize(rt, c);
ans[min(rsz,csz)] = max(ans[min(rsz,csz)], dt+1);
}
void upd_DFS(int c, int dt, int p, int rt) {
for(int i: adj[c]) {
if(vs[i] || i == p) continue;
upd_DFS(i, dt+1, c, rt);
}
if(c == rt) return;
int csz = getSize(c, rt);
tree.update(csz, dt);
}
void ers_DFS(int c, int dt, int p, int rt) {
for(int i: adj[c]) {
if(vs[i] || i == p) continue;
ers_DFS(i, dt+1, c, rt);
}
if(c == rt) return;
int csz = getSize(c, rt);
tree.erase(csz);
}
void centroid_DFS(int c) {
size_2_DFS(c, -1);
int cent = find_cent_DFS(c, -1, sub_sz[c]);
vs[cent] = 1;
for(int j = 0; j < 2; j ++) {
for(int i: adj[cent]) {
if(vs[i]) continue;
ans_DFS(i, 1, cent, cent);
upd_DFS(i, 1, cent, cent);
}
for(int i: adj[cent]) {
ers_DFS(i, 1, cent, cent);
}
reverse(adj[cent].begin(),adj[cent].end());
}
for(int i: adj[cent]) {
if(vs[i]) continue;
centroid_DFS(i);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
tree.init(n);
for(int i = 1, u, v; i < n; i ++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
size_DFS(1,-1);
centroid_DFS(1);
for(int i = n/2; i >= 1; i --) {
ans[i] = max({ans[i], ans[i+1], 1});
}
for(int i = 1; i <= n; i ++) {
if(i%2) cout << "1\n";
else cout << ans[i/2] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10720 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10724 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10720 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10724 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10744 KB |
Output is correct |
22 |
Correct |
21 ms |
10844 KB |
Output is correct |
23 |
Correct |
23 ms |
10952 KB |
Output is correct |
24 |
Correct |
22 ms |
10840 KB |
Output is correct |
25 |
Correct |
20 ms |
10844 KB |
Output is correct |
26 |
Correct |
19 ms |
10836 KB |
Output is correct |
27 |
Correct |
23 ms |
11096 KB |
Output is correct |
28 |
Correct |
20 ms |
10844 KB |
Output is correct |
29 |
Correct |
21 ms |
10844 KB |
Output is correct |
30 |
Correct |
21 ms |
10844 KB |
Output is correct |
31 |
Correct |
20 ms |
10844 KB |
Output is correct |
32 |
Correct |
34 ms |
11080 KB |
Output is correct |
33 |
Correct |
33 ms |
11100 KB |
Output is correct |
34 |
Correct |
727 ms |
10944 KB |
Output is correct |
35 |
Correct |
1290 ms |
10944 KB |
Output is correct |
36 |
Correct |
33 ms |
10840 KB |
Output is correct |
37 |
Correct |
416 ms |
10968 KB |
Output is correct |
38 |
Correct |
1281 ms |
11056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10720 KB |
Output is correct |
9 |
Correct |
2 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
3 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10724 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
2 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10744 KB |
Output is correct |
22 |
Correct |
21 ms |
10844 KB |
Output is correct |
23 |
Correct |
23 ms |
10952 KB |
Output is correct |
24 |
Correct |
22 ms |
10840 KB |
Output is correct |
25 |
Correct |
20 ms |
10844 KB |
Output is correct |
26 |
Correct |
19 ms |
10836 KB |
Output is correct |
27 |
Correct |
23 ms |
11096 KB |
Output is correct |
28 |
Correct |
20 ms |
10844 KB |
Output is correct |
29 |
Correct |
21 ms |
10844 KB |
Output is correct |
30 |
Correct |
21 ms |
10844 KB |
Output is correct |
31 |
Correct |
20 ms |
10844 KB |
Output is correct |
32 |
Correct |
34 ms |
11080 KB |
Output is correct |
33 |
Correct |
33 ms |
11100 KB |
Output is correct |
34 |
Correct |
727 ms |
10944 KB |
Output is correct |
35 |
Correct |
1290 ms |
10944 KB |
Output is correct |
36 |
Correct |
33 ms |
10840 KB |
Output is correct |
37 |
Correct |
416 ms |
10968 KB |
Output is correct |
38 |
Correct |
1281 ms |
11056 KB |
Output is correct |
39 |
Correct |
1731 ms |
33320 KB |
Output is correct |
40 |
Correct |
1711 ms |
32816 KB |
Output is correct |
41 |
Correct |
1729 ms |
33584 KB |
Output is correct |
42 |
Correct |
1754 ms |
34032 KB |
Output is correct |
43 |
Correct |
1813 ms |
33872 KB |
Output is correct |
44 |
Correct |
1747 ms |
33792 KB |
Output is correct |
45 |
Correct |
3385 ms |
39632 KB |
Output is correct |
46 |
Correct |
3871 ms |
44624 KB |
Output is correct |
47 |
Execution timed out |
4081 ms |
33744 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |