This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) begin(a), end(a)
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
int const N = 1 << 18;
int n;
vector<vector<int>> adj;
int sz[N];
bitset<N> rem;
void dfssz(int ver, int par) {
sz[ver] = 1;
for (auto u : adj[ver]) if (u != par && !rem[u]) {
dfssz(u, ver);
sz[ver] += sz[u];
}
}
int getCentroid(int ver, int par, int msz) {
for (auto u : adj[ver]) if (u != par && !rem[u]) {
if (2 * sz[u] > msz) return getCentroid(u, ver, msz);
}
return ver;
}
int ans[N];
struct SegTree {
int val[2 * N];
void upd(int p, int v, int ver, int lx, int rx, bool smx) {
if (rx - lx == 1) {
if (smx) val[ver] = max(val[ver], v);
else val[ver] = v;
return;
}
int mx = (lx + rx) / 2;
if (p < mx) upd(p, v, ver * 2 + 1, lx, mx, smx);
else upd(p, v, ver * 2 + 2, mx, rx, smx);
val[ver] = max(val[ver * 2 + 1], val[ver * 2 + 2]);
}
int query(int l, int r, int ver, int lx, int rx) {
if (rx <= l || r <= lx) return INT_MIN;
if (l <= lx && rx <= r) return val[ver];
int mx = (lx + rx) / 2;
int a = query(l, r, ver * 2 + 1, lx, mx);
int b = query(l, r, ver * 2 + 2, mx, rx);
return max(a, b);
}
int query(int l, int r) { return query(l, r, 0, 0, N); }
void upd(int p, int v, bool smx) { upd(p, v, 0, 0, N, smx); }
};
SegTree mxDist;
int ub;
void dfs(int ver, int par, int dep, bool write) {
if (write) mxDist.upd(sz[ver], dep, true);
else {
ans[sz[ver]] = max(ans[sz[ver]], dep + mxDist.query(sz[ver], ub) + 1);
}
for (auto u : adj[ver]) if (u != par && !rem[u]) {
dfs(u, ver, dep + 1, write);
}
}
void centroid(int start) {
dfssz(start, -1);
int cen = getCentroid(start, -1, sz[start]);
dfssz(cen, -1);
ub = sz[cen] + 5;
for (int i = 0; i < ub; ++i) {
mxDist.upd(i, 0, false);
}
vector<int> tmp;
for (auto u : adj[cen]) if (!rem[u]) {
tmp.push_back(u);
dfs(u, cen, 1, 0);
dfs(u, cen, 1, 1);
}
for (int i = 0; i < ub; ++i) {
mxDist.upd(i, 0, false);
}
reverse(begin(tmp), end(tmp));
for (auto u : tmp) {
dfs(u, cen, 1, 0);
dfs(u, cen, 1, 1);
}
rem[cen] = true;
for (auto u : adj[cen]) {
if (!rem[u]) centroid(u);
}
}
void solve() {
cin >> n;
adj.resize(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
centroid(0);
ans[N-1] = max(ans[N-1], 1);
for (int i = N - 2; i >= 0; --i) ans[i] = max(ans[i], ans[i+1]);
for (int i = 1; i <= n; ++i) {
if (i % 2) cout << 1 << '\n';
else {
cout << ans[i/2] << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |