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;
const int nx=2e5+5;
int n, u, v, sz[nx], lvl[nx], mx[nx], res[nx], used[nx];
vector<int> d[nx];
pair<int, int> dp[nx];
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1;
sz[u]=1;
for (auto v:d[u]) if (v!=p&&!used[v]) dfs(v, u), sz[u]+=sz[v];
mx[sz[u]]=max(mx[sz[u]], lvl[u]);
}
int dfssz(int u, int p)
{
sz[u]=1;
for (auto v:d[u]) if (v!=p&&!used[v]) sz[u]+=dfssz(v, u);
return sz[u];
}
int findcentroid(int u, int p, int rtsz)
{
for (auto v:d[u]) if (v!=p&&!used[v]&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
return u;
}
void decomposition(int u)
{
u=findcentroid(u, u, dfssz(u, u));
used[u]=1;
int sm=sz[u];
for (int i=1; i<=sm; i++) dp[i]={-1, -1};
for (auto v:d[u])
{
if (used[v]) continue;
lvl[v]=0;
dfs(v, v);
for (int i=sz[v]; i>=1; i--)
{
mx[i]=max(mx[i], mx[i+1]);
//cout<<"mx "<<v<<' '<<u<<' '<<i<<' '<<mx[i]<<'\n';
if (mx[i]==0) continue;
if (dp[i].first==-1||dp[i].first<mx[i]) swap(dp[i].first, dp[i].second), dp[i].first=mx[i];
else if (dp[i].second==-1||mx[i]>dp[i].second) dp[i].second=mx[i];
}
for (int i=1; i<=sz[v]; i++) mx[i]=0;
}
for (int i=1; i<=sm; i++)
{
//cout<<"debug "<<u<<' '<<i<<' '<<dp[i].first<<' '<<dp[i].second<<'\n';
if (dp[i].first!=-1&&dp[i].second==-1) res[i]=max(res[i], dp[i].first);
else if (dp[i].first!=-1&&dp[i].second!=-1) res[i]=max(res[i], dp[i].first+dp[i].second);
}
for (auto v:d[u]) if (!used[v]) decomposition(v);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
decomposition(1);
for (int i=1; i<=n; i++) cout<<((i%2)?1:(res[i/2]+1))<<'\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... |