#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define sp << ' ' <<
#define nl << '\n'
const int LIM = 2e5, INF = 1e9;
template<class T> struct SegmentTree{
const T ID = -INF;
int n, i; vector<T> a;
SegmentTree(int N) : a((n=N)*2, ID) {}
SegmentTree& operator[](int j){ i=j+n; return *this; }
void operator=(T v){
for(a[i]=v; i/=2; ) a[i] = max(a[2*i], a[2*i+1]); }
int operator()(int l, int r){
T x = ID;
for(l+=n, r+=n+1; l<r; l/=2, r/=2){
if(l & 1) x = max(x, a[l++]);
if(r & 1) x = max(x, a[--r]);
}
return x;
}
};
int n, ans[LIM], s[LIM];
vector<int> g[LIM];
bool r[LIM], update;
SegmentTree<int> st(LIM);
void init(int u){
s[u] = 1;
for(int v : g[u]) if(!s[v]) init(v), s[u] += s[v];
}
int find(int u){
for(int v : g[u]) if(!r[v] && s[v] > s[u]/2){
s[u] -= s[v], s[v] += s[u];
return find(v);
}
return u;
}
void dfs(int u, int d){
if(update) st[s[u]] = max(st(s[u], s[u]), d);
else ans[s[u]] = max(ans[s[u]], st(s[u], n) + d);
for(int v : g[u]) if(!r[v] && s[v] < s[u]) dfs(v, d+1);
}
void decompose(int u){
r[u = find(u)] = 1;
for(int v : g[u]) if(!r[v]){
ans[s[v]+1] = max(ans[s[v]+1], st(s[v]+1, n) + 0);
update = 0;
dfs(v, 1);
st[s[v]+1] = max(st(s[v]+1, s[v]+1), 0);
update = 1;
dfs(v, 1);
}
for(int i=0; i<s[u]; ++i) st[i+1] = -INF;
for(int v : g[u]) if(!r[v]) decompose(v);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i=1; i<n; ++i){
int u, v; cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
init(0);
decompose(0);
for(int i=n; --i>=0; ) ans[i] = max(ans[i], ans[i+1]);
for(int i=1; i<=n; ++i){
if(i & 1) cout << 1 nl;
else cout << ans[i/2]+1 nl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6604 KB |
Output is correct |
2 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |