#include<bits/stdc++.h>
#define ll long long
#define vt vector
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)v.size()
using namespace std;
const ll base = 107, mod = 1e9 + 7;
const int mxn = 2e5 + 5, inf = 1e9, sq = 400;
int n;
vt<int>adj[mxn + 1];
int dep[mxn + 1], up[mxn + 1][18], ans[mxn + 1], sz[mxn + 1];
int dfs_sz(int s, int pre){
sz[s] = 1;
for(auto i: adj[s]){
if(i != pre){
sz[s] += dfs_sz(i, s);
}
}
return(sz[s]);
}
pii centroid(int s, int pre, int need){
pii cand = make_pair(n, s);
int mxsub = 0;
for(auto i: adj[s]){
if(i != pre){
cand = min(cand, centroid(i, s, need));
mxsub = max(mxsub, sz[i]);
}
}
mxsub = max(mxsub, need - sz[s]);
cand = min(cand, make_pair(mxsub, s));
return(cand);
}
void dfs_lca(int s, int pre){
for(auto i: adj[s]){
if(i != pre){
dep[i] = dep[s] + 1; up[i][0] = s;
dfs_lca(i, s);
}
}
}
int lca(int u, int v){
if(dep[u] < dep[v])swap(u, v);
int k = dep[u] - dep[v];
for(int i = 0; i < 18; i++){
if(k & (1 << i)){
u = up[u][i];
}
}
if(u == v)return(u);
//cout << u << " " << v << " ";
for(int i = 17; i >= 0; i--){
if(up[u][i] != up[v][i]){
u = up[u][i]; v = up[v][i];
}
}
//cout << u << " " << v << " ";
return(up[u][0]);
}
int dist(int u, int v){
return(dep[u] + dep[v] - 2 * dep[lca(u, v)]);
}
vt<int>events[mxn + 1];
int main(){
ios_base::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].pb(v); adj[v].pb(u);
}
int c = centroid(1, -1, dfs_sz(1, -1)).second;
dfs_sz(c, -1);
dfs_lca(c, -1);
for(int i = 1; i < 18; i++){
for(int j = 1; j <= n; j++){
up[j][i] = up[up[j][i - 1]][i - 1];
}
}
//cout << up[10][0];
int ld = c, rd = c, len = 0;
for(int i = 1; i <= n; i++){
events[sz[i]].pb(i);
}
for(int i = n / 2; i >= 1; i--){
for(auto u: events[i]){
//sz[u] will always be the minimum :))
//cout << u << " ";
int expand_rght = dist(ld, u), expand_lft = dist(u, rd);
if(expand_rght > len){
len = expand_rght; rd = u;
}else if(expand_lft > len){
len = expand_lft; ld = u;
}
}
//cout << ld << " " << rd << " " << dist(ld, rd) << "\n";
ans[i] = len + 1;
}
for(int i = 1; i <= n; i++){
if(i & 1)cout << 1 << "\n";
else cout << ans[i / 2] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11888 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11888 KB |
Output is correct |
5 |
Correct |
2 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11864 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
3 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11884 KB |
Output is correct |
11 |
Correct |
2 ms |
11868 KB |
Output is correct |
12 |
Correct |
3 ms |
11868 KB |
Output is correct |
13 |
Correct |
3 ms |
11920 KB |
Output is correct |
14 |
Correct |
3 ms |
11696 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
3 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
3 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
11868 KB |
Output is correct |
21 |
Correct |
3 ms |
11924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11888 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11888 KB |
Output is correct |
5 |
Correct |
2 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11864 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
3 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11884 KB |
Output is correct |
11 |
Correct |
2 ms |
11868 KB |
Output is correct |
12 |
Correct |
3 ms |
11868 KB |
Output is correct |
13 |
Correct |
3 ms |
11920 KB |
Output is correct |
14 |
Correct |
3 ms |
11696 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
3 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
3 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
11868 KB |
Output is correct |
21 |
Correct |
3 ms |
11924 KB |
Output is correct |
22 |
Correct |
6 ms |
13984 KB |
Output is correct |
23 |
Correct |
4 ms |
13992 KB |
Output is correct |
24 |
Correct |
5 ms |
14172 KB |
Output is correct |
25 |
Correct |
4 ms |
14172 KB |
Output is correct |
26 |
Correct |
5 ms |
14172 KB |
Output is correct |
27 |
Correct |
5 ms |
14424 KB |
Output is correct |
28 |
Correct |
6 ms |
13988 KB |
Output is correct |
29 |
Correct |
5 ms |
14072 KB |
Output is correct |
30 |
Correct |
5 ms |
14188 KB |
Output is correct |
31 |
Correct |
5 ms |
14172 KB |
Output is correct |
32 |
Correct |
5 ms |
14184 KB |
Output is correct |
33 |
Correct |
5 ms |
14428 KB |
Output is correct |
34 |
Correct |
5 ms |
14172 KB |
Output is correct |
35 |
Correct |
4 ms |
14172 KB |
Output is correct |
36 |
Correct |
6 ms |
14228 KB |
Output is correct |
37 |
Correct |
5 ms |
14172 KB |
Output is correct |
38 |
Correct |
5 ms |
14340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11868 KB |
Output is correct |
2 |
Correct |
2 ms |
11888 KB |
Output is correct |
3 |
Correct |
2 ms |
11868 KB |
Output is correct |
4 |
Correct |
2 ms |
11888 KB |
Output is correct |
5 |
Correct |
2 ms |
11868 KB |
Output is correct |
6 |
Correct |
3 ms |
11864 KB |
Output is correct |
7 |
Correct |
3 ms |
11868 KB |
Output is correct |
8 |
Correct |
3 ms |
11868 KB |
Output is correct |
9 |
Correct |
3 ms |
11868 KB |
Output is correct |
10 |
Correct |
3 ms |
11884 KB |
Output is correct |
11 |
Correct |
2 ms |
11868 KB |
Output is correct |
12 |
Correct |
3 ms |
11868 KB |
Output is correct |
13 |
Correct |
3 ms |
11920 KB |
Output is correct |
14 |
Correct |
3 ms |
11696 KB |
Output is correct |
15 |
Correct |
3 ms |
11864 KB |
Output is correct |
16 |
Correct |
3 ms |
11868 KB |
Output is correct |
17 |
Correct |
3 ms |
11868 KB |
Output is correct |
18 |
Correct |
2 ms |
11868 KB |
Output is correct |
19 |
Correct |
3 ms |
11868 KB |
Output is correct |
20 |
Correct |
2 ms |
11868 KB |
Output is correct |
21 |
Correct |
3 ms |
11924 KB |
Output is correct |
22 |
Correct |
6 ms |
13984 KB |
Output is correct |
23 |
Correct |
4 ms |
13992 KB |
Output is correct |
24 |
Correct |
5 ms |
14172 KB |
Output is correct |
25 |
Correct |
4 ms |
14172 KB |
Output is correct |
26 |
Correct |
5 ms |
14172 KB |
Output is correct |
27 |
Correct |
5 ms |
14424 KB |
Output is correct |
28 |
Correct |
6 ms |
13988 KB |
Output is correct |
29 |
Correct |
5 ms |
14072 KB |
Output is correct |
30 |
Correct |
5 ms |
14188 KB |
Output is correct |
31 |
Correct |
5 ms |
14172 KB |
Output is correct |
32 |
Correct |
5 ms |
14184 KB |
Output is correct |
33 |
Correct |
5 ms |
14428 KB |
Output is correct |
34 |
Correct |
5 ms |
14172 KB |
Output is correct |
35 |
Correct |
4 ms |
14172 KB |
Output is correct |
36 |
Correct |
6 ms |
14228 KB |
Output is correct |
37 |
Correct |
5 ms |
14172 KB |
Output is correct |
38 |
Correct |
5 ms |
14340 KB |
Output is correct |
39 |
Correct |
150 ms |
36232 KB |
Output is correct |
40 |
Correct |
152 ms |
35532 KB |
Output is correct |
41 |
Correct |
151 ms |
36568 KB |
Output is correct |
42 |
Correct |
147 ms |
36680 KB |
Output is correct |
43 |
Correct |
173 ms |
36556 KB |
Output is correct |
44 |
Correct |
175 ms |
36856 KB |
Output is correct |
45 |
Correct |
235 ms |
44996 KB |
Output is correct |
46 |
Correct |
203 ms |
51108 KB |
Output is correct |
47 |
Correct |
115 ms |
37188 KB |
Output is correct |
48 |
Correct |
96 ms |
37148 KB |
Output is correct |
49 |
Correct |
163 ms |
37432 KB |
Output is correct |
50 |
Correct |
90 ms |
37028 KB |
Output is correct |
51 |
Correct |
161 ms |
48596 KB |
Output is correct |