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>
using namespace std;
int n, u, v, subtree[200005], ans[200005], l[200005], l2[200005];
vector<int> adj[200005];
bool rm[200005];
void build(int u, int par) {
subtree[u] = 1;
for (int node: adj[u]) {
if(node == par || rm[node]) continue;
build(node, u);
subtree[u] += subtree[node];
}
}
int centroid(int u, int par, int sz) {
for (int node: adj[u]) {
if(node == par || rm[node]) continue;
if(subtree[node] > sz / 2) return centroid(node, u, sz);
}
return u;
}
void dfs(int u, int par, int dis) {
l[subtree[u]] = max(l[subtree[u]], dis + 1);
//cout << "CHECK " << u << ' ' << subtree[u] << ' ' << dis + 1 << '\n';
for (int node: adj[u]) {
if(node == par || rm[node]) continue;
dfs(node, u, dis + 1);
}
}
void build2(int u, int par) {
subtree[u] = 1;
for (int node: adj[u]) {
if(node == par || rm[node]) continue;
build2(node, u);
subtree[u] += subtree[node];
}
}
void solve(int u) {
build(u, -1);
int C = centroid(u, -1, subtree[u]);
build2(C, -1);
rm[C] = true;
int mx = 0;
//cout << "C: " << C << '\n';
for (int i = 1; i <= subtree[C]; i++) l2[i] = 1;
for (int node: adj[C]) {
if(rm[node]) continue;
dfs(node, C, 1);
mx = max(mx, subtree[node] + 5);
for (int i = subtree[node] - 1; i >= 1; i--) l[i] = max(l[i], l[i + 1]);
//if(C == 1) cout << l[1] << ' ' << l2[1] << '\n';
for (int i = subtree[node]; i >= 1; i--) {
ans[i * 2] = max(ans[i * 2], l2[i] + l[i] - 1);
l2[i] = max(l2[i], l[i]);
l[i] = 0;
}
}
for (int i = 1; i <= mx; i++) l2[i] = l[i] = 0;
for (int node: adj[C]) {
if(rm[node]) continue;
solve(node);
}
}
signed main() {
//freopen("fact.inp", "r", stdin);
//freopen("fact.out", "w", stdout);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
solve(1);
for (int i = 1; i <= n; i++) {
ans[i] = max(ans[i], 1);
if(i % 2 == 1) cout << 1 << '\n';
else cout << ans[i] << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |