#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;
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];
}
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});
}
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);
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
for(int i = 0; i < n; i++){
sz.assign(n, 1);
ms.clear();
ms.resize(n+1);
s.clear();
s.resize(n);
dfs_sz(i, -1);
dfs(i, -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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |