#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> adj(n+1);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
int centroid = -1;
function<int(int,int)> find_centroid = [&](int x,int p){
int siz = 1;
for(int&i:adj[x])if(i!=p)siz+=find_centroid(i,x);
if(n-siz<=n/2 and siz<=n){
centroid = x;
return (int)1e15;
}
return siz;
};
find_centroid(1,0);
vector<int> depth(n+1);
vector<vector<int>> lift(n+1,vector<int>(18));
vector<pair<int,int>> subsizes;
function<int(int,int,int)> dfs = [&](int x,int p,int dep){
int siz = 1;
lift[x][0] = p;
depth[x] = dep;
for(int&i:adj[x])if(i!=p)siz+=dfs(i,x,dep+1);
subsizes.emplace_back(siz,x);
return siz;
};
dfs(centroid,0,1);
sort(subsizes.rbegin(),subsizes.rend());
for(int bit=1;bit<18;bit++){
for(int i=1;i<=n;i++){
lift[i][bit] = lift[lift[i][bit-1]][bit-1];
}
}
auto iter = subsizes.begin()+1;
int diaL = centroid;
int diaR = centroid;
int diaLen = 1;
auto lca = [&](int a,int b){
if(depth[a]<depth[b])swap(a,b);
int tar = depth[a]-depth[b];
for(int bit=0;bit<18;bit++)if(tar&(1<<bit))a=lift[a][bit];
if(a==b)return a;
for(int bit=17;bit>=0;bit--)if(lift[a][bit]!=lift[b][bit]){
a = lift[a][bit];
b = lift[b][bit];
}
return lift[a][0];
};
auto dist = [&](int a,int b){
return depth[a]+depth[b]-2*depth[lca(a,b)]+1;
};
vector<int> ans(n/2 + 1);
for(int i=n/2;i;i--){
while(iter!=subsizes.end() and iter->first>=i){
// Add iter
if(dist(iter->second,diaL)>diaLen){
diaR = iter->second;
diaLen++;
} else if(dist(iter->second,diaR)>diaLen){
diaL = iter->second;
diaLen++;
}
iter++;
}
ans[i] = diaLen;
}
for(int i=1;i<=n;i++){
if(i&1)cout<<"1\n";
else cout<<ans[i/2]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
1628 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1624 KB |
Output is correct |
28 |
Correct |
3 ms |
1424 KB |
Output is correct |
29 |
Correct |
4 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
4 ms |
1496 KB |
Output is correct |
32 |
Correct |
3 ms |
1628 KB |
Output is correct |
33 |
Correct |
3 ms |
1880 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
2 ms |
1488 KB |
Output is correct |
36 |
Correct |
3 ms |
1880 KB |
Output is correct |
37 |
Correct |
3 ms |
1876 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
452 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
4 ms |
1628 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1624 KB |
Output is correct |
28 |
Correct |
3 ms |
1424 KB |
Output is correct |
29 |
Correct |
4 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
4 ms |
1496 KB |
Output is correct |
32 |
Correct |
3 ms |
1628 KB |
Output is correct |
33 |
Correct |
3 ms |
1880 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
2 ms |
1488 KB |
Output is correct |
36 |
Correct |
3 ms |
1880 KB |
Output is correct |
37 |
Correct |
3 ms |
1876 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
39 |
Correct |
180 ms |
56772 KB |
Output is correct |
40 |
Correct |
182 ms |
56768 KB |
Output is correct |
41 |
Correct |
180 ms |
56776 KB |
Output is correct |
42 |
Correct |
202 ms |
58080 KB |
Output is correct |
43 |
Correct |
186 ms |
57028 KB |
Output is correct |
44 |
Correct |
200 ms |
58184 KB |
Output is correct |
45 |
Correct |
302 ms |
64968 KB |
Output is correct |
46 |
Correct |
210 ms |
69580 KB |
Output is correct |
47 |
Correct |
158 ms |
57156 KB |
Output is correct |
48 |
Correct |
133 ms |
58952 KB |
Output is correct |
49 |
Correct |
199 ms |
57284 KB |
Output is correct |
50 |
Correct |
131 ms |
59208 KB |
Output is correct |
51 |
Correct |
172 ms |
67656 KB |
Output is correct |