#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+6;
vector<int>g[MAXN];
bool used[MAXN];
int ans[MAXN];
int n;
int subtree[MAXN];
int max1=1;
int dfs(int u,int p)
{
int val=1;
for(auto xd:g[u])
{
if(xd==p)continue;
int t=dfs(xd,u);
max1=max(max1,2*min(t,n-t));
val+=t;
}
return val;
}
void dfs1(int u,int p)
{
subtree[u]=1;
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
dfs1(xd,u);
subtree[u]+=subtree[xd];
}
}
int find_centroid(int u,int p,int n1)
{
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
if(subtree[xd]>n1/2)
{
return find_centroid(xd,u,n1);
}
}
return u;
}
struct BIT
{
int fen[2*MAXN];
void update(int idx,int val)
{
idx++;
for(;idx<2*MAXN;idx+=idx&(-idx))
{
fen[idx]=max(fen[idx],val);
}
}
void wipe(int idx)
{
idx++;
for(;idx<2*MAXN;idx+=idx&(-idx))
{
fen[idx]=0;
}
}
int query(int idx)
{
idx++;
int ret=0;
for(;idx>0;idx-=idx&(-idx))
{
ret=max(ret,fen[idx]);
}
return ret;
}
}fen;
void dfs_query(int u,int p,int l)
{
int t=subtree[u];
//cout<<"query "<<u<<" "<<t<<" "<<l<<endl;
//cout<<l+fen.query(MAXN-t)+1<<endl;
ans[2*t]=max(ans[2*t],l+fen.query(MAXN-t)+1);
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
dfs_query(xd,u,l+1);
}
}
void dfs_update(int u,int p,int l)
{
int t=subtree[u];
fen.update(MAXN-t,l);
//cout<<"update "<<u<<" "<<t<<" "<<l<<endl;
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
dfs_update(xd,u,l+1);
}
}
void dfs_wipe(int u,int p)
{
int t=subtree[u];
fen.wipe(MAXN-t);
for(auto xd:g[u])
{
if(xd==p)continue;
if(used[xd])continue;
dfs_wipe(xd,u);
}
}
void decompose(int u)
{
dfs1(u,0);
int c=find_centroid(u,0,subtree[u]);
//cout<<" "<<c<<endl;
dfs1(c,0);
used[c]=1;
for(auto xd:g[c])
{
if(used[xd])continue;
dfs_query(xd,c,1);
dfs_update(xd,c,1);
}
for(auto xd:g[c])
{
if(used[xd])continue;
dfs_wipe(xd,c);
}
//cout<<"-------------\n";
reverse(g[c].begin(),g[c].end());
for(auto xd:g[c])
{
if(used[xd])continue;
dfs_query(xd,c,1);
dfs_update(xd,c,1);
}
for(auto xd:g[c])
{
if(used[xd])continue;
dfs_wipe(xd,c);
}
for(auto xd:g[c])
{
if(used[xd]==0)
{
decompose(xd);
}
}
}
int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=1;i<=n;i++)
{
ans[i]=1;
}
dfs(1,0);
for(int i=2;i<=max1;i+=2)
{
ans[i]=2;
}
decompose(1);
for(int i=n;i>=3;i--)
{
ans[i-2]=max(ans[i-2],ans[i]);
}
for(int i=1;i<=n;i++)
{
cout<<ans[i]<<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4972 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
6 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4972 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
6 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
22 ms |
5228 KB |
Output is correct |
23 |
Correct |
22 ms |
5248 KB |
Output is correct |
24 |
Correct |
21 ms |
5228 KB |
Output is correct |
25 |
Correct |
21 ms |
5228 KB |
Output is correct |
26 |
Correct |
21 ms |
5228 KB |
Output is correct |
27 |
Correct |
21 ms |
5228 KB |
Output is correct |
28 |
Correct |
21 ms |
5356 KB |
Output is correct |
29 |
Correct |
21 ms |
5376 KB |
Output is correct |
30 |
Correct |
22 ms |
5356 KB |
Output is correct |
31 |
Correct |
21 ms |
5228 KB |
Output is correct |
32 |
Correct |
26 ms |
5356 KB |
Output is correct |
33 |
Correct |
25 ms |
5484 KB |
Output is correct |
34 |
Correct |
17 ms |
5328 KB |
Output is correct |
35 |
Correct |
19 ms |
5228 KB |
Output is correct |
36 |
Correct |
21 ms |
5356 KB |
Output is correct |
37 |
Correct |
17 ms |
5228 KB |
Output is correct |
38 |
Correct |
20 ms |
5356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4972 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
6 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
5 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
22 ms |
5228 KB |
Output is correct |
23 |
Correct |
22 ms |
5248 KB |
Output is correct |
24 |
Correct |
21 ms |
5228 KB |
Output is correct |
25 |
Correct |
21 ms |
5228 KB |
Output is correct |
26 |
Correct |
21 ms |
5228 KB |
Output is correct |
27 |
Correct |
21 ms |
5228 KB |
Output is correct |
28 |
Correct |
21 ms |
5356 KB |
Output is correct |
29 |
Correct |
21 ms |
5376 KB |
Output is correct |
30 |
Correct |
22 ms |
5356 KB |
Output is correct |
31 |
Correct |
21 ms |
5228 KB |
Output is correct |
32 |
Correct |
26 ms |
5356 KB |
Output is correct |
33 |
Correct |
25 ms |
5484 KB |
Output is correct |
34 |
Correct |
17 ms |
5328 KB |
Output is correct |
35 |
Correct |
19 ms |
5228 KB |
Output is correct |
36 |
Correct |
21 ms |
5356 KB |
Output is correct |
37 |
Correct |
17 ms |
5228 KB |
Output is correct |
38 |
Correct |
20 ms |
5356 KB |
Output is correct |
39 |
Correct |
1844 ms |
16236 KB |
Output is correct |
40 |
Correct |
1739 ms |
15960 KB |
Output is correct |
41 |
Correct |
1817 ms |
16236 KB |
Output is correct |
42 |
Correct |
1722 ms |
16492 KB |
Output is correct |
43 |
Correct |
1832 ms |
16460 KB |
Output is correct |
44 |
Correct |
1771 ms |
16492 KB |
Output is correct |
45 |
Correct |
2669 ms |
21740 KB |
Output is correct |
46 |
Correct |
2037 ms |
25716 KB |
Output is correct |
47 |
Correct |
841 ms |
16700 KB |
Output is correct |
48 |
Correct |
733 ms |
16864 KB |
Output is correct |
49 |
Correct |
1601 ms |
17388 KB |
Output is correct |
50 |
Correct |
809 ms |
16868 KB |
Output is correct |
51 |
Correct |
1355 ms |
24368 KB |
Output is correct |