#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=200030;
const int mxM=500004;
const int MOD=998244353;
const ll INF=8e18;
typedef struct node{
int p, m, pm, mp, pmp;
}node;
node mrg(node a, node b){
node c;
c.p=max(a.p, b.p);
c.m=max(a.m, b.m);
c.pm=max(a.p+b.m, max(a.pm, b.pm));
c.mp=max(a.m+b.p, max(a.mp, b.mp));
c.pmp=max(a.pm+b.p, max(a.p+b.mp, max(a.pmp, b.pmp)));
return c;
}
node seg[8*mxN];
void upd(int idx, int s, int e, int pos, int val){
if(s==e){
seg[idx].p=val;
seg[idx].m=-2*val;
seg[idx].pm=seg[idx].mp=-val;
seg[idx].pmp=0;
return;
}
int mid=(s+e)/2;
if(pos<=mid) upd(2*idx, s, mid, pos, val);
else upd(2*idx+1, mid+1, e, pos, val);
seg[idx]=mrg(seg[2*idx], seg[2*idx+1]);
}
int N;
vector <int> v[mxN];
int sub[mxN], dep[mxN];
vector <int> in[mxN];
int ei;
vector <int> ct[mxN];
int ans[mxN];
void input(){
cin >> N;
for(int i=1;i<N;i++){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
}
void dfs1(int now, int pre){
sub[now]=1;
in[now].push_back(++ei);
for(int nxt : v[now]) if(nxt!=pre) {
dep[nxt]=dep[now]+1;
dfs1(nxt, now);
sub[now]+=sub[nxt];
in[now].push_back(++ei);
}
}
void find_cent(){
dfs1(1, -1);
int now=1;
while(true){
bool esc=true;
for(int nxt : v[now]){
if(sub[nxt]<sub[now] && sub[nxt]>=(N+1)/2){
now=nxt;
esc=false;
break;
}
}
if(esc) break;
}
ei=0;
dep[now]=0;
for(int i=1;i<=N;i++) in[i].clear();
dfs1(now, -1);
for(int i=1;i<=N;i++) ct[sub[i]].push_back(i);
for(int i=0;i<8*mxN;i++) seg[i].p=seg[i].m=seg[i].pm=seg[i].mp=seg[i].pmp=-1e8;
for(int x : in[now]) upd(1, 1, 2*N, x, dep[now]);
//for(int i=1;i<=N;i++) printf("dep[%d]=%d, sub[%d], in[%d]=%d\n", i, dep[i], i, sub[i], i, in[i]);
}
void solv(){
for(int i=N;i>=1;i--){
if(i%2){
ans[i]=1;
continue;
}
//printf("i=%d\n", i);
for(int x : ct[i/2]) for(int y : in[x]) upd(1, 1, 2*N, y, dep[x]);
//printf("\n");
ans[i]=seg[1].pmp+1;
}
}
int main()
{
gibon
input();
find_cent();
solv();
for(int i=1;i<=N;i++) cout << ans[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
47960 KB |
Output is correct |
2 |
Correct |
9 ms |
47964 KB |
Output is correct |
3 |
Correct |
9 ms |
47964 KB |
Output is correct |
4 |
Correct |
9 ms |
47964 KB |
Output is correct |
5 |
Correct |
10 ms |
47964 KB |
Output is correct |
6 |
Correct |
9 ms |
47964 KB |
Output is correct |
7 |
Correct |
9 ms |
47964 KB |
Output is correct |
8 |
Correct |
9 ms |
47964 KB |
Output is correct |
9 |
Correct |
9 ms |
47964 KB |
Output is correct |
10 |
Correct |
9 ms |
47964 KB |
Output is correct |
11 |
Correct |
9 ms |
47960 KB |
Output is correct |
12 |
Correct |
9 ms |
47964 KB |
Output is correct |
13 |
Correct |
9 ms |
47812 KB |
Output is correct |
14 |
Correct |
9 ms |
47964 KB |
Output is correct |
15 |
Correct |
9 ms |
47868 KB |
Output is correct |
16 |
Correct |
10 ms |
47964 KB |
Output is correct |
17 |
Correct |
9 ms |
47816 KB |
Output is correct |
18 |
Correct |
9 ms |
47960 KB |
Output is correct |
19 |
Correct |
9 ms |
47960 KB |
Output is correct |
20 |
Correct |
9 ms |
47884 KB |
Output is correct |
21 |
Correct |
9 ms |
48216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
47960 KB |
Output is correct |
2 |
Correct |
9 ms |
47964 KB |
Output is correct |
3 |
Correct |
9 ms |
47964 KB |
Output is correct |
4 |
Correct |
9 ms |
47964 KB |
Output is correct |
5 |
Correct |
10 ms |
47964 KB |
Output is correct |
6 |
Correct |
9 ms |
47964 KB |
Output is correct |
7 |
Correct |
9 ms |
47964 KB |
Output is correct |
8 |
Correct |
9 ms |
47964 KB |
Output is correct |
9 |
Correct |
9 ms |
47964 KB |
Output is correct |
10 |
Correct |
9 ms |
47964 KB |
Output is correct |
11 |
Correct |
9 ms |
47960 KB |
Output is correct |
12 |
Correct |
9 ms |
47964 KB |
Output is correct |
13 |
Correct |
9 ms |
47812 KB |
Output is correct |
14 |
Correct |
9 ms |
47964 KB |
Output is correct |
15 |
Correct |
9 ms |
47868 KB |
Output is correct |
16 |
Correct |
10 ms |
47964 KB |
Output is correct |
17 |
Correct |
9 ms |
47816 KB |
Output is correct |
18 |
Correct |
9 ms |
47960 KB |
Output is correct |
19 |
Correct |
9 ms |
47960 KB |
Output is correct |
20 |
Correct |
9 ms |
47884 KB |
Output is correct |
21 |
Correct |
9 ms |
48216 KB |
Output is correct |
22 |
Correct |
13 ms |
48220 KB |
Output is correct |
23 |
Correct |
13 ms |
48220 KB |
Output is correct |
24 |
Correct |
14 ms |
48220 KB |
Output is correct |
25 |
Correct |
13 ms |
48208 KB |
Output is correct |
26 |
Correct |
13 ms |
48220 KB |
Output is correct |
27 |
Correct |
13 ms |
48220 KB |
Output is correct |
28 |
Correct |
13 ms |
48216 KB |
Output is correct |
29 |
Correct |
13 ms |
48220 KB |
Output is correct |
30 |
Correct |
13 ms |
48216 KB |
Output is correct |
31 |
Correct |
13 ms |
48220 KB |
Output is correct |
32 |
Correct |
13 ms |
48472 KB |
Output is correct |
33 |
Correct |
13 ms |
48476 KB |
Output is correct |
34 |
Correct |
13 ms |
48220 KB |
Output is correct |
35 |
Correct |
13 ms |
48220 KB |
Output is correct |
36 |
Correct |
13 ms |
48220 KB |
Output is correct |
37 |
Correct |
13 ms |
48220 KB |
Output is correct |
38 |
Correct |
13 ms |
48476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
47960 KB |
Output is correct |
2 |
Correct |
9 ms |
47964 KB |
Output is correct |
3 |
Correct |
9 ms |
47964 KB |
Output is correct |
4 |
Correct |
9 ms |
47964 KB |
Output is correct |
5 |
Correct |
10 ms |
47964 KB |
Output is correct |
6 |
Correct |
9 ms |
47964 KB |
Output is correct |
7 |
Correct |
9 ms |
47964 KB |
Output is correct |
8 |
Correct |
9 ms |
47964 KB |
Output is correct |
9 |
Correct |
9 ms |
47964 KB |
Output is correct |
10 |
Correct |
9 ms |
47964 KB |
Output is correct |
11 |
Correct |
9 ms |
47960 KB |
Output is correct |
12 |
Correct |
9 ms |
47964 KB |
Output is correct |
13 |
Correct |
9 ms |
47812 KB |
Output is correct |
14 |
Correct |
9 ms |
47964 KB |
Output is correct |
15 |
Correct |
9 ms |
47868 KB |
Output is correct |
16 |
Correct |
10 ms |
47964 KB |
Output is correct |
17 |
Correct |
9 ms |
47816 KB |
Output is correct |
18 |
Correct |
9 ms |
47960 KB |
Output is correct |
19 |
Correct |
9 ms |
47960 KB |
Output is correct |
20 |
Correct |
9 ms |
47884 KB |
Output is correct |
21 |
Correct |
9 ms |
48216 KB |
Output is correct |
22 |
Correct |
13 ms |
48220 KB |
Output is correct |
23 |
Correct |
13 ms |
48220 KB |
Output is correct |
24 |
Correct |
14 ms |
48220 KB |
Output is correct |
25 |
Correct |
13 ms |
48208 KB |
Output is correct |
26 |
Correct |
13 ms |
48220 KB |
Output is correct |
27 |
Correct |
13 ms |
48220 KB |
Output is correct |
28 |
Correct |
13 ms |
48216 KB |
Output is correct |
29 |
Correct |
13 ms |
48220 KB |
Output is correct |
30 |
Correct |
13 ms |
48216 KB |
Output is correct |
31 |
Correct |
13 ms |
48220 KB |
Output is correct |
32 |
Correct |
13 ms |
48472 KB |
Output is correct |
33 |
Correct |
13 ms |
48476 KB |
Output is correct |
34 |
Correct |
13 ms |
48220 KB |
Output is correct |
35 |
Correct |
13 ms |
48220 KB |
Output is correct |
36 |
Correct |
13 ms |
48220 KB |
Output is correct |
37 |
Correct |
13 ms |
48220 KB |
Output is correct |
38 |
Correct |
13 ms |
48476 KB |
Output is correct |
39 |
Correct |
322 ms |
64624 KB |
Output is correct |
40 |
Correct |
322 ms |
64176 KB |
Output is correct |
41 |
Correct |
323 ms |
64716 KB |
Output is correct |
42 |
Correct |
331 ms |
64968 KB |
Output is correct |
43 |
Correct |
339 ms |
64952 KB |
Output is correct |
44 |
Correct |
326 ms |
65224 KB |
Output is correct |
45 |
Correct |
339 ms |
72440 KB |
Output is correct |
46 |
Correct |
297 ms |
77652 KB |
Output is correct |
47 |
Correct |
292 ms |
65688 KB |
Output is correct |
48 |
Correct |
261 ms |
65868 KB |
Output is correct |
49 |
Correct |
293 ms |
66268 KB |
Output is correct |
50 |
Correct |
282 ms |
66028 KB |
Output is correct |
51 |
Correct |
281 ms |
75900 KB |
Output is correct |