#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first
#define vs second
const int mod = 1000000007;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 200004;
vector<int> adj[N];
int siz[N];
bool vis[N];
int ans[N];
int mx[N];
void cal_siz(int node , int par){
siz[node] = 1;
for(int i : adj[node]){
if(i!=par && !vis[i]){
cal_siz(i , node);
siz[node] += siz[i];
}
}
}
int centroid(int node , int par , int tot){
for(int i : adj[node]){
if(!vis[i] && i != par && siz[i] >= tot){
return centroid(i , node , tot);
}
}
return node;
}
void dfs(int node , int par , int dis){
mx[siz[node]] = max(mx[siz[node]] , dis);
for(int i : adj[node]){
if(i!=par && !vis[i])dfs(i , node , dis + 1);
}
}
void centroid_decomposition(int node){
cal_siz(node , node);
int tot = siz[node];
int root = centroid(node , node , (tot)/2);
cal_siz(root,root);
vector<pair<int,int>> v[tot + 2];
int mx_siz = 0;
for(int i : adj[root]){
if(!vis[i]){
mx_siz = max(mx_siz , siz[i]);
for(int j = 0; j <= siz[i]; j++){
mx[j] = -1;
}
dfs(i , root , 1);
for(int j = 1; j <= siz[i]; j++){
//cout << mx[j] << ' ';
v[j].pb(mp(mx[j],i));
sort(all(v[j]),greater<>());
while(v[j].size() > 2)v[j].pop_back();
}
//cout << '\n';
}
}
v[tot-mx_siz].pb(mp(0,-1));
sort(all(v[tot-mx_siz]) , greater<>());
while(v[tot-mx_siz].size() > 2)v[tot-mx_siz].pop_back();
/*for(int i = tot; i >= 0; i--){
cout << i << '\n';
for(auto j : v[tot]){
cout << j.vf << ' ' << j.vs << '\n';
}
}*/
for(int i = tot; i > 0; i--){
for(auto j : v[i + 1]){
v[i].pb(j);
}
assert(v[i].size() <= 4);
sort(all(v[i]),greater<>());
if(v[i].size() >= 2){
if(v[i][0].vs == v[i][1].vs){
v[i].erase(v[i].begin() + 1);
}
}
if(v[i].size() >= 2){
if(v[i][0].vs == v[i][1].vs){
v[i].erase(v[i].begin()+1);
}
}
if(v[i].size() >= 2){
if(v[i][0].vs == v[i][1].vs){
v[i].erase(v[i].begin()+1);
}
}
while(v[i].size() > 2)v[i].pop_back();
if(v[i].size() >= 2 && v[i][0].vs != v[i][1].vs){
ans[2*i] = max(ans[2*i] , v[i][0].vf + v[i][1].vf + 1);
}
}
vis[root] = 1;
for(int i : adj[root]){
if(!vis[i]){
centroid_decomposition(i);
}
}
}
void solve(){
int n;
cin >> n;
for(int i = 0; i < n - 1; i++){
int a , b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
memset(vis,0,sizeof vis);
memset(ans,-1,sizeof ans);
centroid_decomposition(1);
for(int i = n; i > 0; i--){
if(i % 2 == 1){;}
else {
ans[i] = max(ans[i] , ans[i + 2]);
}
}
for(int i = 1; i <= n; i++){
if(i % 2 == 1)cout << 1 << '\n';
else cout << max(1,ans[i]) << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;
//cin >> t;
//while(t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
1 ms |
7516 KB |
Output is correct |
3 |
Correct |
1 ms |
7516 KB |
Output is correct |
4 |
Correct |
1 ms |
7516 KB |
Output is correct |
5 |
Correct |
1 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
1 ms |
7524 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
1 ms |
7512 KB |
Output is correct |
10 |
Correct |
1 ms |
7516 KB |
Output is correct |
11 |
Correct |
1 ms |
7516 KB |
Output is correct |
12 |
Correct |
1 ms |
7528 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
1 ms |
7536 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
16 |
Correct |
1 ms |
7512 KB |
Output is correct |
17 |
Correct |
1 ms |
7516 KB |
Output is correct |
18 |
Correct |
1 ms |
7516 KB |
Output is correct |
19 |
Correct |
1 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
1 ms |
7516 KB |
Output is correct |
3 |
Correct |
1 ms |
7516 KB |
Output is correct |
4 |
Correct |
1 ms |
7516 KB |
Output is correct |
5 |
Correct |
1 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
1 ms |
7524 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
1 ms |
7512 KB |
Output is correct |
10 |
Correct |
1 ms |
7516 KB |
Output is correct |
11 |
Correct |
1 ms |
7516 KB |
Output is correct |
12 |
Correct |
1 ms |
7528 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
1 ms |
7536 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
16 |
Correct |
1 ms |
7512 KB |
Output is correct |
17 |
Correct |
1 ms |
7516 KB |
Output is correct |
18 |
Correct |
1 ms |
7516 KB |
Output is correct |
19 |
Correct |
1 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7528 KB |
Output is correct |
22 |
Correct |
4 ms |
8028 KB |
Output is correct |
23 |
Correct |
5 ms |
8028 KB |
Output is correct |
24 |
Correct |
5 ms |
8072 KB |
Output is correct |
25 |
Correct |
4 ms |
8068 KB |
Output is correct |
26 |
Correct |
4 ms |
7984 KB |
Output is correct |
27 |
Correct |
4 ms |
7772 KB |
Output is correct |
28 |
Correct |
5 ms |
8056 KB |
Output is correct |
29 |
Correct |
5 ms |
8028 KB |
Output is correct |
30 |
Correct |
5 ms |
8028 KB |
Output is correct |
31 |
Correct |
5 ms |
8028 KB |
Output is correct |
32 |
Correct |
5 ms |
8052 KB |
Output is correct |
33 |
Correct |
5 ms |
8024 KB |
Output is correct |
34 |
Correct |
3 ms |
7708 KB |
Output is correct |
35 |
Correct |
3 ms |
7772 KB |
Output is correct |
36 |
Correct |
4 ms |
7876 KB |
Output is correct |
37 |
Correct |
4 ms |
8028 KB |
Output is correct |
38 |
Correct |
4 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
1 ms |
7516 KB |
Output is correct |
3 |
Correct |
1 ms |
7516 KB |
Output is correct |
4 |
Correct |
1 ms |
7516 KB |
Output is correct |
5 |
Correct |
1 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
1 ms |
7524 KB |
Output is correct |
8 |
Correct |
2 ms |
7512 KB |
Output is correct |
9 |
Correct |
1 ms |
7512 KB |
Output is correct |
10 |
Correct |
1 ms |
7516 KB |
Output is correct |
11 |
Correct |
1 ms |
7516 KB |
Output is correct |
12 |
Correct |
1 ms |
7528 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
1 ms |
7536 KB |
Output is correct |
15 |
Correct |
2 ms |
7512 KB |
Output is correct |
16 |
Correct |
1 ms |
7512 KB |
Output is correct |
17 |
Correct |
1 ms |
7516 KB |
Output is correct |
18 |
Correct |
1 ms |
7516 KB |
Output is correct |
19 |
Correct |
1 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7528 KB |
Output is correct |
22 |
Correct |
4 ms |
8028 KB |
Output is correct |
23 |
Correct |
5 ms |
8028 KB |
Output is correct |
24 |
Correct |
5 ms |
8072 KB |
Output is correct |
25 |
Correct |
4 ms |
8068 KB |
Output is correct |
26 |
Correct |
4 ms |
7984 KB |
Output is correct |
27 |
Correct |
4 ms |
7772 KB |
Output is correct |
28 |
Correct |
5 ms |
8056 KB |
Output is correct |
29 |
Correct |
5 ms |
8028 KB |
Output is correct |
30 |
Correct |
5 ms |
8028 KB |
Output is correct |
31 |
Correct |
5 ms |
8028 KB |
Output is correct |
32 |
Correct |
5 ms |
8052 KB |
Output is correct |
33 |
Correct |
5 ms |
8024 KB |
Output is correct |
34 |
Correct |
3 ms |
7708 KB |
Output is correct |
35 |
Correct |
3 ms |
7772 KB |
Output is correct |
36 |
Correct |
4 ms |
7876 KB |
Output is correct |
37 |
Correct |
4 ms |
8028 KB |
Output is correct |
38 |
Correct |
4 ms |
8028 KB |
Output is correct |
39 |
Correct |
256 ms |
34256 KB |
Output is correct |
40 |
Correct |
257 ms |
32912 KB |
Output is correct |
41 |
Correct |
266 ms |
33244 KB |
Output is correct |
42 |
Correct |
255 ms |
33624 KB |
Output is correct |
43 |
Correct |
251 ms |
33616 KB |
Output is correct |
44 |
Correct |
242 ms |
32340 KB |
Output is correct |
45 |
Correct |
435 ms |
36060 KB |
Output is correct |
46 |
Correct |
369 ms |
39248 KB |
Output is correct |
47 |
Correct |
88 ms |
28104 KB |
Output is correct |
48 |
Correct |
79 ms |
28244 KB |
Output is correct |
49 |
Correct |
264 ms |
36300 KB |
Output is correct |
50 |
Correct |
110 ms |
34504 KB |
Output is correct |
51 |
Correct |
249 ms |
37732 KB |
Output is correct |