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 int long long
#define ll pair<int,int>
using namespace std;
const int N=400022;
set<int> ke[N];
int maxx[N];
int h[N];
int kq[N];
int oo=1e9;
int kq2[N];
int sz[N];
void dfs(int u,int pa)
{
sz[u]=1;
for (int v : ke[u])
{
if (v==pa) continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
int dfs2(int u,int pa,int kk)
{
for (int v : ke[u])
{
if (v==pa) continue;
if (sz[v]>kk/2) return dfs2(v,u,kk);
}
return u;
}
void dfs3(int u,int pa,int kk)
{
sz[u]=1;
h[u]=kk;
for (int v : ke[u])
{
if (v==pa) continue;
dfs3(v,u,kk+1);
sz[u]+=sz[v];
}
kq[sz[u]]=max(kq[sz[u]],h[u]);
}
void gg(int u)
{
dfs(u,0);
int dinh=dfs2(u,0,sz[u]);
h[dinh]=0;
for (int i=1;i<=sz[u];i++) kq[i]=-oo;
maxx[dinh]=0;
int cur=1;
for (int v : ke[dinh])
{
dfs3(v,dinh,1);
kq[sz[v]+1]=0;
for (int i2=sz[v]+1;i2>=2;i2--) kq[i2-1]=max(kq[i2-1],kq[i2]);
for (int i2=1;i2<=sz[v]+1;i2++)
{
kq2[i2*2]=max(kq2[i2*2],kq[i2]+maxx[i2]);
}
for (int i2=1;i2<=sz[v]+1;i2++)
{
maxx[i2]=max(maxx[i2],kq[i2]);
kq[i2]=-oo;
}
for (int i2=cur;i2<=cur+sz[v];i2++) maxx[i2]=max(maxx[i2],0ll);
cur+=sz[v];
}
set<int> lol=ke[dinh];
set<int> :: iterator itr;
for (itr=lol.begin();itr!=lol.end();itr++)
{
int v=*itr;
ke[v].erase(dinh);
ke[dinh].erase(v);
gg(v);
}
}
signed main()
{
// freopen("kk.inp","r",stdin);
// freopen("kk.ans","w",stdout);
// freopen("City.inp","r",stdin);
// freopen("City.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i=1;i<n;i++)
{
int u,v;
cin >> u >> v;
ke[u].insert(v);
ke[v].insert(u);
}
gg(1);
for (int i=1;i<=n;i++)
{
cout << kq2[i]+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... |