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 par[200005][20], sz[200005], dep[200005], ans[200005], n, c, x, y, ind, res=1;
vector<pair<int, int> > vec;
vector<int> adj[200005];
void dfs(int u, int p)
{
sz[u]=1;
for (int v:adj[u])
{
if (v!=p)
{
dfs(v, u);
sz[u]+=sz[v];
}
}
bool ok=1;
for (int v:adj[u])
{
if (sz[v]>n/2)
{
ok=0;
break;
}
}
if (ok && n-sz[u]<=n/2)
c=x=y=u;
}
void dfs2(int u, int p)
{
par[u][0]=p;
for (int i=1; i<20; i++)
par[u][i]=par[par[u][i-1]][i-1];
sz[u]=1;
for (int v:adj[u])
{
if (v!=p)
{
dep[v]=dep[u]+1;
dfs2(v, u);
sz[u]+=sz[v];
}
}
}
int lca(int u, int v)
{
if (dep[u]<dep[v])
swap(u, v);
if (dep[u]>dep[v])
{
int diff=dep[u]-dep[v];
for (int i=0; i<20; i++)
if (diff&(1<<i))
u=par[u][i];
}
if (u==v)
return u;
for (int i=19; i>=0; i--)
if (par[u][i]!=par[v][i])
u=par[u][i], v=par[v][i];
return par[u][0];
}
int len(int u, int v)
{
return dep[u]+dep[v]-dep[lca(u, v)]*2;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
dfs2(c, c);
for (int i=1; i<=n; i++)
if (i!=c)
vec.push_back({min(sz[i], n-sz[i]+1), i});
sort(vec.begin(), vec.end(), greater<pair<int, int> >());
for (int i=n; i>=1; i--)
{
if (i&1)
{
ans[i]=1;
continue;
}
while (ind<vec.size() && vec[ind].first>=i/2)
{
int u=vec[ind].second;
int xy=len(x, y);
int ux=len(u, x);
int uy=len(u, y);
if (ux>=uy && ux>xy)
{
y=u;
res=ux+1;
}
else if (uy>ux && uy>xy)
{
x=u;
res=uy+1;
}
ind++;
}
ans[i]=res;
}
for (int i=1; i<=n; i++)
cout << ans[i] << '\n';
}
Compilation message (stderr)
meetings2.cpp: In function 'int main()':
meetings2.cpp:93:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | while (ind<vec.size() && vec[ind].first>=i/2)
| ~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |