#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxN = 2e5 + 5;
const int logN = log2(maxN)+1;
vector<int> adj[maxN];
int parent[logN][maxN], sz[maxN], depth[maxN];
int c[maxN];
void dfs(int curNode, int prvNode, int d = 1){
parent[0][curNode] = prvNode;
for(int x=1;x<logN;x++){
parent[x][curNode] = parent[x-1][parent[x-1][curNode]];
}
sz[curNode] = 1;
depth[curNode] = d;
for(auto nxt : adj[curNode]){
if(nxt != prvNode){
dfs(nxt, curNode, d+1);
sz[curNode] += sz[nxt];
}
}
}
int dfs2(int curNode, int prvNode){
for(auto nxt : adj[curNode]){
if(nxt != prvNode && sz[nxt] > sz[0]/2){
return dfs2(nxt, curNode);
}
}
return curNode;
}
int findLCA(int a, int b){
if(depth[a] > depth[b]) swap(a, b);
for(int x=logN-1;x>=0;x--){
if(depth[a] <= depth[b]-(1 << x)){
b = parent[x][b];
}
}
if(a == b) return a;
for(int x=logN-1;x>=0;x--){
if(parent[x][a] != parent[x][b]){
a = parent[x][a];
b = parent[x][b];
}
}
return parent[0][a];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int x=0;x<n-1;x++){
int a, b;
cin >> a >> b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0, 0);
int center = dfs2(0, 0);
dfs(center, center);
int l = center, r = center;
vector<pair<int, int>> v;
for(int x=0;x<n;x++){
v.push_back({sz[x], x});
}
sort(v.rbegin(), v.rend());
int ans[n+1], cur = 1;
fill(ans, ans+n+1, 0);
for(int x=0;x<n;x++){
int idx = v[x].second;
int d1 = depth[l]+depth[idx]-2*depth[findLCA(l, idx)]+1;
int d2 = depth[r]+depth[idx]-2*depth[findLCA(r, idx)]+1;
if(d1 < d2){
swap(d1, d2);
swap(l, r);
}
if(d1 > cur){
cur = d1;
r = idx;
}
ans[v[x].first] = cur;
}
for(int x=n-1;x>=0;x--){
ans[x] = max(ans[x], ans[x+1]);
}
for(int x=1;x<=n;x++){
if(x&1){
cout << "1\n";
} else {
cout << ans[x>>1] << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
5 ms |
5588 KB |
Output is correct |
23 |
Correct |
7 ms |
5676 KB |
Output is correct |
24 |
Correct |
6 ms |
5588 KB |
Output is correct |
25 |
Correct |
5 ms |
5588 KB |
Output is correct |
26 |
Correct |
5 ms |
5588 KB |
Output is correct |
27 |
Correct |
5 ms |
5680 KB |
Output is correct |
28 |
Correct |
5 ms |
5588 KB |
Output is correct |
29 |
Correct |
5 ms |
5680 KB |
Output is correct |
30 |
Correct |
5 ms |
5680 KB |
Output is correct |
31 |
Correct |
6 ms |
5588 KB |
Output is correct |
32 |
Correct |
6 ms |
5716 KB |
Output is correct |
33 |
Correct |
5 ms |
5844 KB |
Output is correct |
34 |
Correct |
5 ms |
5588 KB |
Output is correct |
35 |
Correct |
5 ms |
5588 KB |
Output is correct |
36 |
Correct |
6 ms |
5716 KB |
Output is correct |
37 |
Correct |
5 ms |
5716 KB |
Output is correct |
38 |
Correct |
5 ms |
5672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
3 ms |
5028 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
2 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5028 KB |
Output is correct |
22 |
Correct |
5 ms |
5588 KB |
Output is correct |
23 |
Correct |
7 ms |
5676 KB |
Output is correct |
24 |
Correct |
6 ms |
5588 KB |
Output is correct |
25 |
Correct |
5 ms |
5588 KB |
Output is correct |
26 |
Correct |
5 ms |
5588 KB |
Output is correct |
27 |
Correct |
5 ms |
5680 KB |
Output is correct |
28 |
Correct |
5 ms |
5588 KB |
Output is correct |
29 |
Correct |
5 ms |
5680 KB |
Output is correct |
30 |
Correct |
5 ms |
5680 KB |
Output is correct |
31 |
Correct |
6 ms |
5588 KB |
Output is correct |
32 |
Correct |
6 ms |
5716 KB |
Output is correct |
33 |
Correct |
5 ms |
5844 KB |
Output is correct |
34 |
Correct |
5 ms |
5588 KB |
Output is correct |
35 |
Correct |
5 ms |
5588 KB |
Output is correct |
36 |
Correct |
6 ms |
5716 KB |
Output is correct |
37 |
Correct |
5 ms |
5716 KB |
Output is correct |
38 |
Correct |
5 ms |
5672 KB |
Output is correct |
39 |
Correct |
310 ms |
32028 KB |
Output is correct |
40 |
Correct |
283 ms |
31432 KB |
Output is correct |
41 |
Correct |
276 ms |
32108 KB |
Output is correct |
42 |
Correct |
300 ms |
32548 KB |
Output is correct |
43 |
Correct |
285 ms |
32456 KB |
Output is correct |
44 |
Correct |
286 ms |
32460 KB |
Output is correct |
45 |
Correct |
499 ms |
36520 KB |
Output is correct |
46 |
Correct |
323 ms |
40760 KB |
Output is correct |
47 |
Correct |
225 ms |
32848 KB |
Output is correct |
48 |
Correct |
179 ms |
33048 KB |
Output is correct |
49 |
Correct |
353 ms |
33088 KB |
Output is correct |
50 |
Correct |
205 ms |
33096 KB |
Output is correct |
51 |
Correct |
264 ms |
39600 KB |
Output is correct |