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;
#define ff first
#define ss second
#define bl " "
#define endl "\n"
#define all(v) v.begin(), v.end()
#define comp(v) v.erase(unique(all(v)), v.end())
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<pi, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 200200;
const int MOD = 998244353;
vector<int> adj[MAXN];
priority_queue<pi> pq[MAXN], q;
set<pi> st;
bool vst[MAXN];
int res[MAXN], num[MAXN], tmp[MAXN];
int dst[MAXN], cnt[MAXN];
void dfs1(int here, int par) {
cnt[here] = 1;
if(vst[here]) return;
for(int there : adj[here]) {
if(there == par) continue;
dfs1(there, here);
cnt[here] += cnt[there];
}
}
void dfs2(int here, int par, int t) {
if(!vst[here]) {
for(int there : adj[here]) {
if(there == par) continue;
dst[there] = dst[here] + 1;
dfs2(there, here, t);
}
}
int x = (num[here] < num[par] ? num[here] : num[1]-num[par]);
pq[t].push(pi(x, dst[here]));
}
void solve(int here) {
dfs1(here, here);
int n = cnt[here];
for(int par = here; ; ) {
bool flag = true;
for(int there : adj[here]) {
if(there == par || vst[there]) continue;
if(cnt[there]*2 > n) {
flag = false;
par = here; here = there;
break;
}
}
if(flag) break;
}
dst[here] = 0; int t = 0;
for(int there : adj[here]) {
while(pq[t].size()) pq[t].pop();
dst[there] = dst[here] + 1;
dfs2(there, here, t);
tmp[t] = 0;
t++;
}
while(q.size()) q.pop();
//cout<<here<<endl;
for(int i = 0; i < t; i++) q.push(pi(pq[i].top().ff, i));
st.clear();
int mx = q.top().ff;
while(q.size()) {
while(q.size() && q.top().ff == mx) {
pi x = q.top(); q.pop();
if(st.find(pi(tmp[x.ss], x.ss)) != st.end()) st.erase(pi(tmp[x.ss], x.ss));
tmp[x.ss] = max(tmp[x.ss], pq[x.ss].top().ss);
st.insert(pi(tmp[x.ss], x.ss));
pq[x.ss].pop();
if(pq[x.ss].size()) q.push(pi(pq[x.ss].top().ff, x.ss));
}
//cout<<mx<<bl<<it->ff<<bl<<next(it)->ff<<endl;
if(st.size() > 1) {
auto it = prev(st.end());
res[mx] = max(res[mx], it->ff+prev(it)->ff+1);
}
if(q.size()) mx = q.top().ff;
}
vst[here] = 1;
for(int there : adj[here]) {
if(!vst[there])
solve(there);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs1(1, 1);
for(int i = 1; i <= n; i++) num[i] = cnt[i];
int idx = 0;
for(int i = 2; i <= n; i++)
idx = max(idx, min(cnt[i], n-cnt[i]));
res[n] = 1; res[idx] = 2;
solve(1);
for(int i = n-1; i; i--) res[i] = max(res[i], res[i+1]);
for(int i = 1; i <= n; i++) cout << (i%2 ? 1 : res[i/2]) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |