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;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int x, y; cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
vector<bool> used(n + 1);
vector<int> sz(n + 1), d(n + 1);
function<void(int, int)> fill_sz = [&](int v, int pr){
sz[v] = 1;
for (int i: g[v]){
if (i == pr || used[i]) continue;
d[i] = d[v] + 1;
fill_sz(i, v);
sz[v] += sz[i];
}
};
function<int(int, int, int&)> find = [&](int v, int pr, int& S){
for (int i: g[v]){
if (i == pr || used[i]) continue;
if (2 * sz[i] >= S){
return find(i, v, S);
}
}
return v;
};
vector<pii> all;
function<void(int, int, int&)> add = [&](int v, int pr, int& u){
all.pb({v, u});
for (int i: g[v]){
if (i == pr || used[i]) continue;
add(i, v, u);
}
};
vector<int> out(n + 1), p(n + 1);
vector<pii> dd[n + 1];
multiset<int> st;
function<void(int)> arvid = [&](int v){
fill_sz(v, 0);
v = find(v, 0, sz[v]);
used[v] = 1;
d[v] = 0;
fill_sz(v, 0);
all.clear();
for (int i: g[v]){
if (used[i]) continue;
add(i, 0, i);
p[i] = 0;
}
int f = sz[v] / 2;
for (int i = 1; i <= f; i++) dd[i].clear();
for (auto [x, y]: all){
dd[sz[x]].pb({x, y});
}
for (int i = f; i > 0; i--){
for (auto [x, y]: dd[i]){
if (p[y] < d[x]){
if (!p[y]){
st.insert(d[x]);
}
else {
st.erase(st.find(p[y]));
st.insert(d[x]);
}
p[y] = d[x];
}
}
if (st.size() < 2) continue;
auto it = prev(st.end());
int sum = *it + *prev(it) + 1;
out[i] = max(out[i], sum);
}
for (auto [x, y]: all){
int k = min(sz[x], sz[v] - sz[y]);
out[k] = max(out[k], d[x] + 1);
}
for (int i: g[v]){
if (used[i]) continue;
arvid(i);
}
};
arvid(1);
for (int i = 1; i <= n; i++){
cout<<((i % 2) ? 1 : out[i / 2])<<"\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... |