#include<bits/stdc++.h>
using namespace std;
const int INF = 1e8;
int n;
int parent[200005],siz[200005],depth[200005];
vector<int> con[200005];
int anc[200005][18];
int cine[400005];
int tin[200005],tout[200005],timer;
vector<int> unde[200005];
void dfs(int nod)
{
tin[nod]=++timer;
cine[timer]=nod;
unde[nod].push_back(timer);
siz[nod]=1;
for(auto adj:con[nod])
{
if(adj!=parent[nod])
{
parent[adj]=nod;
anc[adj][0]=nod;
depth[adj]=depth[nod]+1;
dfs(adj);
cine[++timer]=nod;
unde[nod].push_back(timer);
siz[nod]+=siz[adj];
}
}
tout[nod]=timer;
}
struct node
{
int a,b,ab,bc,abc;
};
node combine(node x, node y)
{
node aux;
aux.a = max(x.a, y.a);
aux.b = max(x.b, y.b);
aux.ab = max({x.ab, y.ab, x.a+y.b});
aux.bc = max({x.bc, y.bc, x.b+y.a});
aux.abc = max({x.abc, y.abc, x.a+y.bc, x.ab+y.a});
return aux;
}
node aint[1100000];
void build(int nod, int st, int dr)
{
if(st==dr)
{
aint[nod] = {-INF,-INF,-INF,-INF,-INF};
return;
}
int mij=(st+dr)/2;
build(nod*2,st,mij);
build(nod*2+1,mij+1,dr);
aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
void upd(int nod, int st, int dr, int poz, int newv)
{
if(st==dr)
{
aint[nod].a = newv;
aint[nod].b = -2*newv;
aint[nod].ab = aint[nod].bc = aint[nod].abc = -INF;
return;
}
int mij=(st+dr)/2;
if(poz<=mij) upd(nod*2,st,mij,poz,newv);
else upd(nod*2+1,mij+1,dr,poz,newv);
aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
node qry(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return {-INF,-INF,-INF,-INF,-INF};
if(le==st && dr==ri)
return aint[nod];
int mij=(st+dr)/2;
return combine(qry(nod*2,st,mij,le,min(mij,ri)),
qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
int rez[200005];
signed main()
{
cin>>n;
int a,b;
for(int i=1;i<n;i++)
{
cin>>a>>b;
con[a].push_back(b);
con[b].push_back(a);
}
dfs(1);
build(1,1,timer);
for(int p=1;p<18;p++)
for(int i=1;i<=n;i++)
anc[i][p] = anc[anc[i][p-1]][p-1];
vector<pair<pair<int,int>,int>> v;
for(int i=1;i<=n;i++)
{
v.push_back({{-siz[i],0},i});
v.push_back({{-(n-siz[i]),1},i});
}
sort(v.begin(),v.end());
for(auto x:v)
{
int i=x.second;
int tip=x.first.second;
//cout<<i<<" "<<tip<<" zzz\n";
if(tip==0)
{
for(auto x:unde[i])
upd(1,1,timer,x,depth[i]);
int cap=i;
while(cap!=1 && n-siz[cap]>=siz[i])
cap = parent[cap];
rez[siz[i]] = max(rez[siz[i]], qry(1,1,timer,tin[cap],tout[cap]).abc+1);
}
else
{
rez[n-siz[i]] = max(rez[n-siz[i]], qry(1,1,timer,tin[i],tout[i]).a-depth[i]+2);
}
}
for(int i=n-1;i>0;i--)
rez[i] = max({1, rez[i], rez[i+1]});
for(int i=1;i<=n;i++)
{
if(i%2==1)
{
cout<<1<<"\n";
}
else
{
cout<<rez[i/2]<<"\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9700 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9872 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9700 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9872 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9776 KB |
Output is correct |
22 |
Correct |
11 ms |
11100 KB |
Output is correct |
23 |
Correct |
12 ms |
11104 KB |
Output is correct |
24 |
Correct |
14 ms |
11100 KB |
Output is correct |
25 |
Correct |
12 ms |
11096 KB |
Output is correct |
26 |
Correct |
11 ms |
11036 KB |
Output is correct |
27 |
Correct |
12 ms |
11100 KB |
Output is correct |
28 |
Correct |
11 ms |
11056 KB |
Output is correct |
29 |
Correct |
11 ms |
11100 KB |
Output is correct |
30 |
Correct |
13 ms |
11100 KB |
Output is correct |
31 |
Correct |
12 ms |
11100 KB |
Output is correct |
32 |
Correct |
17 ms |
11100 KB |
Output is correct |
33 |
Correct |
21 ms |
11104 KB |
Output is correct |
34 |
Correct |
11 ms |
11104 KB |
Output is correct |
35 |
Correct |
10 ms |
10892 KB |
Output is correct |
36 |
Correct |
13 ms |
11096 KB |
Output is correct |
37 |
Correct |
10 ms |
11100 KB |
Output is correct |
38 |
Correct |
13 ms |
11044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9816 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9820 KB |
Output is correct |
11 |
Correct |
4 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
4 ms |
9700 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
4 ms |
9696 KB |
Output is correct |
17 |
Correct |
5 ms |
9816 KB |
Output is correct |
18 |
Correct |
4 ms |
9816 KB |
Output is correct |
19 |
Correct |
4 ms |
9872 KB |
Output is correct |
20 |
Correct |
4 ms |
9820 KB |
Output is correct |
21 |
Correct |
4 ms |
9776 KB |
Output is correct |
22 |
Correct |
11 ms |
11100 KB |
Output is correct |
23 |
Correct |
12 ms |
11104 KB |
Output is correct |
24 |
Correct |
14 ms |
11100 KB |
Output is correct |
25 |
Correct |
12 ms |
11096 KB |
Output is correct |
26 |
Correct |
11 ms |
11036 KB |
Output is correct |
27 |
Correct |
12 ms |
11100 KB |
Output is correct |
28 |
Correct |
11 ms |
11056 KB |
Output is correct |
29 |
Correct |
11 ms |
11100 KB |
Output is correct |
30 |
Correct |
13 ms |
11100 KB |
Output is correct |
31 |
Correct |
12 ms |
11100 KB |
Output is correct |
32 |
Correct |
17 ms |
11100 KB |
Output is correct |
33 |
Correct |
21 ms |
11104 KB |
Output is correct |
34 |
Correct |
11 ms |
11104 KB |
Output is correct |
35 |
Correct |
10 ms |
10892 KB |
Output is correct |
36 |
Correct |
13 ms |
11096 KB |
Output is correct |
37 |
Correct |
10 ms |
11100 KB |
Output is correct |
38 |
Correct |
13 ms |
11044 KB |
Output is correct |
39 |
Correct |
579 ms |
70852 KB |
Output is correct |
40 |
Correct |
571 ms |
70284 KB |
Output is correct |
41 |
Correct |
604 ms |
71108 KB |
Output is correct |
42 |
Correct |
633 ms |
71560 KB |
Output is correct |
43 |
Correct |
613 ms |
71620 KB |
Output is correct |
44 |
Correct |
590 ms |
71876 KB |
Output is correct |
45 |
Execution timed out |
4057 ms |
75968 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |