#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 |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
3 ms |
1628 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1368 KB |
Output is correct |
27 |
Correct |
4 ms |
1628 KB |
Output is correct |
28 |
Correct |
2 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1420 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1668 KB |
Output is correct |
33 |
Correct |
3 ms |
1744 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
2 ms |
1628 KB |
Output is correct |
36 |
Correct |
2 ms |
1628 KB |
Output is correct |
37 |
Correct |
2 ms |
1628 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
3 ms |
1628 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1368 KB |
Output is correct |
27 |
Correct |
4 ms |
1628 KB |
Output is correct |
28 |
Correct |
2 ms |
1628 KB |
Output is correct |
29 |
Correct |
3 ms |
1420 KB |
Output is correct |
30 |
Correct |
3 ms |
1628 KB |
Output is correct |
31 |
Correct |
3 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1668 KB |
Output is correct |
33 |
Correct |
3 ms |
1744 KB |
Output is correct |
34 |
Correct |
2 ms |
1372 KB |
Output is correct |
35 |
Correct |
2 ms |
1628 KB |
Output is correct |
36 |
Correct |
2 ms |
1628 KB |
Output is correct |
37 |
Correct |
2 ms |
1628 KB |
Output is correct |
38 |
Correct |
2 ms |
1628 KB |
Output is correct |
39 |
Correct |
183 ms |
57452 KB |
Output is correct |
40 |
Correct |
188 ms |
56384 KB |
Output is correct |
41 |
Correct |
176 ms |
57280 KB |
Output is correct |
42 |
Correct |
173 ms |
58952 KB |
Output is correct |
43 |
Correct |
195 ms |
58788 KB |
Output is correct |
44 |
Correct |
170 ms |
57260 KB |
Output is correct |
45 |
Correct |
278 ms |
64192 KB |
Output is correct |
46 |
Correct |
204 ms |
68660 KB |
Output is correct |
47 |
Correct |
142 ms |
57664 KB |
Output is correct |
48 |
Correct |
125 ms |
58724 KB |
Output is correct |
49 |
Correct |
214 ms |
58824 KB |
Output is correct |
50 |
Correct |
132 ms |
58696 KB |
Output is correct |
51 |
Correct |
151 ms |
67652 KB |
Output is correct |