#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n;
vector<vector<int>> v;
vector<int> ans;
vector<set<pair<int, int>>> s;
vector<int> sz;
vector<multiset<int>> ms;
vector<vector<int>> anc;
int get_anc(int nd, int ss){
for(int i = 1; i < 20; i++){
if(ss%2 == 1) nd = anc[nd][i];
if(nd == -1) return -1;
ss/=2;
}
return nd;
}
int sth(int nd, int szz, int dis){
if(dis == 0) return -1;
int a = 0, b = dis;
if(sz[nd] > szz) return -1;
while(a < b){
int md = (a+b+1)/2;
if(sz[get_anc(nd, md)] > szz) b = md-1;
else a = md;
}
return a;
}
void dfs_sz(int nd, int ss){
for(int x: v[nd]) if(x != ss) dfs_sz(x, nd);
for(int x: v[nd]) if(x != ss) sz[nd]+=sz[x];
anc[nd][1] = ss;
}
void dfs(int nd, int ss, int dis, bool keep){
int mx = 0, in = -1;
for(int x: v[nd]) if(x != ss && sz[x] > mx) mx = sz[x], in = x;
for(int x: v[nd]) if(x != ss && x != in) dfs(x, nd, dis+1, 0);
if(in != -1){
dfs(in, nd, dis+1, 1);
swap(s[nd], s[in]);
}
for(int x: v[nd]) if(x != ss){
for(auto [szz0, d0]: s[x]){
int d = d0, szz = szz0;
if(n-sz[x] >= szz) ans[szz] = max(ans[szz], d-dis+1);
if(!ms[szz].empty()){
int cur = *ms[szz].rbegin();
cur+=d;
cur-=2*dis;
cur++;
ans[szz] = max(ans[szz], cur);
}
}
for(auto [szz0, d0]: s[x]){
int d = d0, szz = szz0;
auto it = s[nd].lower_bound(make_pair(szz, 0));
if(it == s[nd].end() || it->f != szz){
s[nd].insert({szz, d});
ms[szz].insert(d);
}
else if(it->sc < d){
ms[szz].erase(ms[szz].find(it->sc));
s[nd].erase(it);
s[nd].insert({szz, d});
ms[szz].insert(d);
}
}
s[x].clear();
}
int szz = n-sz[nd]+1;
if(!ms[szz].empty()) ans[szz] = max(ans[szz], (*ms[szz].rbegin())-dis+1);
for(int i = mx+1; i <= sz[nd]; i++){
ms[i].insert(dis);
s[nd].insert({i, dis});
int anc = sth(nd, n-i, dis);
if(anc != -1){
anc+=2;
ans[i] = max(ans[i], anc);
}
}
if(!keep){
for(auto [szz0, d0]: s[nd]){
int d = d0, szz = szz0;
ms[szz].erase(ms[szz].find(d));
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
v.resize(n);
ans.resize(n+1, 1);
sz.resize(n, 1);
s.resize(n);
ms.resize(n+1);
anc.resize(n, vector<int>(20, -1));
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
dfs_sz(0, -1);
for(int i = 0; i < n; i++) anc[i][0] = i;
for(int i = 2; i < 20; i++) for(int j = 0; j < n; j++){
if(anc[j][i-1] == -1) continue;
anc[j][i] = anc[anc[j][i-1]][i-1];
}
dfs(0, -1, 0, 1);
for(int i = n-1; i >= 0; i--) ans[i] = max(ans[i], ans[i+1]);
for(int i = 1; i <= n; i++){
if(i%2 == 0) cout << ans[i/2] << "\n";
else cout << "1\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
208 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
208 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
9 ms |
1752 KB |
Output is correct |
23 |
Correct |
9 ms |
1780 KB |
Output is correct |
24 |
Correct |
10 ms |
1780 KB |
Output is correct |
25 |
Correct |
11 ms |
1744 KB |
Output is correct |
26 |
Correct |
9 ms |
1740 KB |
Output is correct |
27 |
Correct |
9 ms |
1824 KB |
Output is correct |
28 |
Correct |
10 ms |
1740 KB |
Output is correct |
29 |
Correct |
9 ms |
1732 KB |
Output is correct |
30 |
Correct |
9 ms |
1824 KB |
Output is correct |
31 |
Correct |
9 ms |
1732 KB |
Output is correct |
32 |
Correct |
9 ms |
2104 KB |
Output is correct |
33 |
Correct |
8 ms |
2252 KB |
Output is correct |
34 |
Correct |
6 ms |
1740 KB |
Output is correct |
35 |
Correct |
6 ms |
1780 KB |
Output is correct |
36 |
Correct |
9 ms |
1868 KB |
Output is correct |
37 |
Correct |
7 ms |
1740 KB |
Output is correct |
38 |
Correct |
9 ms |
1948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
208 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
9 ms |
1752 KB |
Output is correct |
23 |
Correct |
9 ms |
1780 KB |
Output is correct |
24 |
Correct |
10 ms |
1780 KB |
Output is correct |
25 |
Correct |
11 ms |
1744 KB |
Output is correct |
26 |
Correct |
9 ms |
1740 KB |
Output is correct |
27 |
Correct |
9 ms |
1824 KB |
Output is correct |
28 |
Correct |
10 ms |
1740 KB |
Output is correct |
29 |
Correct |
9 ms |
1732 KB |
Output is correct |
30 |
Correct |
9 ms |
1824 KB |
Output is correct |
31 |
Correct |
9 ms |
1732 KB |
Output is correct |
32 |
Correct |
9 ms |
2104 KB |
Output is correct |
33 |
Correct |
8 ms |
2252 KB |
Output is correct |
34 |
Correct |
6 ms |
1740 KB |
Output is correct |
35 |
Correct |
6 ms |
1780 KB |
Output is correct |
36 |
Correct |
9 ms |
1868 KB |
Output is correct |
37 |
Correct |
7 ms |
1740 KB |
Output is correct |
38 |
Correct |
9 ms |
1948 KB |
Output is correct |
39 |
Correct |
831 ms |
73112 KB |
Output is correct |
40 |
Correct |
829 ms |
71580 KB |
Output is correct |
41 |
Correct |
835 ms |
73160 KB |
Output is correct |
42 |
Correct |
822 ms |
74424 KB |
Output is correct |
43 |
Correct |
873 ms |
74512 KB |
Output is correct |
44 |
Correct |
871 ms |
74584 KB |
Output is correct |
45 |
Correct |
1119 ms |
86924 KB |
Output is correct |
46 |
Correct |
973 ms |
97444 KB |
Output is correct |
47 |
Correct |
421 ms |
74792 KB |
Output is correct |
48 |
Correct |
386 ms |
75040 KB |
Output is correct |
49 |
Correct |
760 ms |
75220 KB |
Output is correct |
50 |
Correct |
511 ms |
75096 KB |
Output is correct |
51 |
Correct |
988 ms |
94548 KB |
Output is correct |