#include <bits/stdc++.h>
using namespace std;
int n,sz[200005],level[200005],p[200005][19];
vector<int> adj[200005],group[200005];
void dfs_sz(int x, int par) {
sz[x]=1;
for (auto s : adj[x]) {
if (s==par) continue;
dfs_sz(s,x);
sz[x]+=sz[s];
}
}
int find_cen(int x, int par) {
for (auto s : adj[x]) if (s!=par && sz[s]>n/2) return find_cen(s,x);
return x;
}
void dfs(int x, int par) {
p[x][0]=par;
level[x]=level[par]+1;
group[sz[x]].push_back(x);
for (int i=1; i<19; ++i) p[x][i]=p[p[x][i-1]][i-1];
for (auto s : adj[x]) if (s!=par) dfs(s,x);
}
int lca(int x, int y) {
if (level[x]<level[y]) swap(x,y);
int dif=level[x]-level[y];
for (int i=0; i<19; ++i) {
if (dif&(1<<i)) {
x=p[x][i];
}
}
if (x==y) return x;
for (int i=18; i>=0; --i) {
if (p[x][i]!=p[y][i]) {
x=p[x][i];
y=p[y][i];
}
}
return p[x][0];
}
int dis(int x, int y) {
return level[x]+level[y]-2*level[lca(x,y)];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for (int i=0; i<n-1; ++i) {
int a,b; cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs_sz(1,0);
int cen=find_cen(1,0);
dfs_sz(cen,0);
dfs(cen,0);
vector<int> ans(n+1,1);
int x=cen,y=cen,dia=0;
for (int i=n/2; i>=1; --i) {
for (auto s : group[i]) {
int d1=dis(s,x), d2=dis(s,y);
if (d1<d2) swap(d1,d2), swap(x,y);
if (d1>dia) dia=d1, y=s;
}
ans[2*i]=dia+1;
}
for (int i=1; i<=n; ++i) cout<<ans[i]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9728 KB |
Output is correct |
11 |
Correct |
4 ms |
9724 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9724 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9736 KB |
Output is correct |
18 |
Correct |
5 ms |
9728 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9728 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9728 KB |
Output is correct |
11 |
Correct |
4 ms |
9724 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9724 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9736 KB |
Output is correct |
18 |
Correct |
5 ms |
9728 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9728 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
10196 KB |
Output is correct |
23 |
Correct |
6 ms |
10248 KB |
Output is correct |
24 |
Correct |
10 ms |
10248 KB |
Output is correct |
25 |
Correct |
6 ms |
10196 KB |
Output is correct |
26 |
Correct |
6 ms |
10196 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
13 ms |
10220 KB |
Output is correct |
30 |
Correct |
7 ms |
10252 KB |
Output is correct |
31 |
Correct |
9 ms |
10196 KB |
Output is correct |
32 |
Correct |
10 ms |
10380 KB |
Output is correct |
33 |
Correct |
8 ms |
10504 KB |
Output is correct |
34 |
Correct |
8 ms |
10184 KB |
Output is correct |
35 |
Correct |
9 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10252 KB |
Output is correct |
37 |
Correct |
8 ms |
10252 KB |
Output is correct |
38 |
Correct |
6 ms |
10376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9692 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9728 KB |
Output is correct |
11 |
Correct |
4 ms |
9724 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9724 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9736 KB |
Output is correct |
18 |
Correct |
5 ms |
9728 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9728 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
8 ms |
10196 KB |
Output is correct |
23 |
Correct |
6 ms |
10248 KB |
Output is correct |
24 |
Correct |
10 ms |
10248 KB |
Output is correct |
25 |
Correct |
6 ms |
10196 KB |
Output is correct |
26 |
Correct |
6 ms |
10196 KB |
Output is correct |
27 |
Correct |
7 ms |
10196 KB |
Output is correct |
28 |
Correct |
7 ms |
10196 KB |
Output is correct |
29 |
Correct |
13 ms |
10220 KB |
Output is correct |
30 |
Correct |
7 ms |
10252 KB |
Output is correct |
31 |
Correct |
9 ms |
10196 KB |
Output is correct |
32 |
Correct |
10 ms |
10380 KB |
Output is correct |
33 |
Correct |
8 ms |
10504 KB |
Output is correct |
34 |
Correct |
8 ms |
10184 KB |
Output is correct |
35 |
Correct |
9 ms |
10196 KB |
Output is correct |
36 |
Correct |
7 ms |
10252 KB |
Output is correct |
37 |
Correct |
8 ms |
10252 KB |
Output is correct |
38 |
Correct |
6 ms |
10376 KB |
Output is correct |
39 |
Correct |
238 ms |
36824 KB |
Output is correct |
40 |
Correct |
252 ms |
36140 KB |
Output is correct |
41 |
Correct |
200 ms |
36952 KB |
Output is correct |
42 |
Correct |
292 ms |
37240 KB |
Output is correct |
43 |
Correct |
262 ms |
37304 KB |
Output is correct |
44 |
Correct |
230 ms |
37448 KB |
Output is correct |
45 |
Correct |
352 ms |
43944 KB |
Output is correct |
46 |
Correct |
263 ms |
48428 KB |
Output is correct |
47 |
Correct |
153 ms |
37760 KB |
Output is correct |
48 |
Correct |
142 ms |
37728 KB |
Output is correct |
49 |
Correct |
208 ms |
37920 KB |
Output is correct |
50 |
Correct |
136 ms |
37828 KB |
Output is correct |
51 |
Correct |
178 ms |
46208 KB |
Output is correct |