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;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define pb emplace_back
#define f first
#define s second
#define mp make_pair
#define SZ(x) (ll)x.size()
#define ALL(x) x.begin(),x.end()
#define lb lower_bound
#define ub upper_bound
const ll MAXN=200100;
const int MAXK=19;
ll N,a,b,c,Q;
vi V[MAXN];
ll sub[MAXN];
ll small[MAXN];
ll A[MAXN];
ll out[MAXN];
int p[MAXN][MAXK];
int d[MAXN];
ll dfs(ll x,ll pa){
sub[x]=1;
p[x][0]=pa;
if(pa!=-1)d[x]=d[pa]+1;
for(int i=1;i<MAXK;++i){
if(p[x][i-1]!=-1)p[x][i]=p[p[x][i-1]][i-1];
}
for(auto v:V[x])if(v!=pa)sub[x]+=dfs(v,x);
return sub[x];
}
set<pi> S;
int lca(int a,int b){
if(a==b)return a;
if(d[a]>d[b])swap(a,b); // d[a] < d[b]
int hd=d[b]-d[a];
for(int i=0;i<MAXK;++i)if((1<<i)&hd){
b=p[b][i];
}
if(a==b)return a;
for(int i=MAXK-1;i>=0;--i){
if(p[a][i]!=p[b][i]){a=p[a][i];b=p[b][i];}
}
assert(p[a][0]==p[b][0]);
return p[a][0];
}
int fd(int x,int y){
return d[x]+d[y]-d[lca(x,y)];
}
pi T;
int ans;
void addNode(int x){
if(T.f==-1)T=mp(x,x);
else{
int a=fd(x,T.f);
int b=fd(x,T.s);
if(a>=ans&&a>=b){
T=mp(x,T.f);
}else if(b>=a&&b>=ans){
T=mp(x,T.s);
}
ans=max({ans,a,b});
}
assert(!A[x]);
A[x]=1;
for(auto v:V[x])if(!A[v]){
S.insert(mp(small[v],v));
}
}
int main(){
T=mp(-1,-1);
memset(p,-1,sizeof(p));
cin>>N;
for(ll i=1;i<N;++i){
cin>>a>>b;
V[a].pb(b);
V[b].pb(a);
}
dfs(1,-1);
for(ll i=1;i<=N;++i){
b=N-sub[i];
for(auto j:V[i])if(j!=p[i][0]){
b=max(b,sub[j]);
}
small[i]=N-b; // everything except biggest subtree
}
// for(ll i=1;i<=N;++i)cerr<<small[i]<<' ';cerr<<'\n';
vpi nodes;
for(ll i=1;i<=N;++i)nodes.pb(small[i],i);
sort(ALL(nodes));
S.insert(nodes.back());
for(ll i=(N/2)*2; i>=2;i-=2){
while(SZ(S)){
pi t=*(--S.end());
if(t.f<i/2)break;
S.erase(--S.end());
addNode(t.s);
}
out[i]=ans;
}
for(int i=1;i<=N;++i){
if(i%2)cout<<1<<'\n';
else cout<<out[i]<<'\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... |