// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const int Log = 20;
vector<int> G[N];
vector< pair<int, int> > E[N];
int n;
int sp[N][Log];
int sub[N], dep[N];
int DFS(int u, int p, int d){
sp[u][0] = p;
sub[u] = 1;
dep[u] = d;
for(int l = 1; l < Log; l++)
sp[u][l] = sp[ sp[u][l - 1] ][l - 1];
for(auto adj : G[u])
if(adj != p)
sub[u] += DFS(adj, u, d + 1);
E[min(n - sub[u], sub[u])].pb({u, p});
return sub[u];
}
int LCA(int u, int v){
if(dep[u] < dep[v]) swap(u, v);
for(int i = 0; i < Log; i++)
if((dep[u] - dep[v]) >> i & 1)
u = sp[u][i];
if(u == v) return u;
for(int l = Log - 1; l >= 0; l--)
if(sp[u][l] != sp[v][l])
u = sp[u][l], v = sp[v][l];
return sp[u][0];
}
int dist(int u, int v){ return dep[u] + dep[v] - 2 * dep[LCA(u, v)]; }
int ans[N];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
DFS(1, 0, 0);
u = -1, v = -1;
for(int i = n; i >= 1; i--){
if(i & 1){
ans[i] = 1;
continue;
}
for(auto [fr, to] : E[i / 2]){
if(u == -1) u = fr, v = to;
if(dist(u, v) < dist(u, fr))
v = fr;
if(dist(u, v) < dist(v, fr))
u = fr;
if(dist(u, v) < dist(u, to))
v = to;
if(dist(u, v) < dist(v, to))
u = to;
}
ans[i] = u == -1 ? 1 : dist(u, v) + 1;
}
for(int i = 1; i <= n; i++)
cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9720 KB |
Output is correct |
2 |
Correct |
7 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9716 KB |
Output is correct |
4 |
Correct |
8 ms |
9716 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9736 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9716 KB |
Output is correct |
19 |
Correct |
6 ms |
9724 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9720 KB |
Output is correct |
2 |
Correct |
7 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9716 KB |
Output is correct |
4 |
Correct |
8 ms |
9716 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9736 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9716 KB |
Output is correct |
19 |
Correct |
6 ms |
9724 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
13 ms |
10308 KB |
Output is correct |
23 |
Correct |
11 ms |
10316 KB |
Output is correct |
24 |
Correct |
11 ms |
10320 KB |
Output is correct |
25 |
Correct |
11 ms |
10316 KB |
Output is correct |
26 |
Correct |
12 ms |
10312 KB |
Output is correct |
27 |
Correct |
11 ms |
10316 KB |
Output is correct |
28 |
Correct |
11 ms |
10296 KB |
Output is correct |
29 |
Correct |
11 ms |
10240 KB |
Output is correct |
30 |
Correct |
11 ms |
10316 KB |
Output is correct |
31 |
Correct |
11 ms |
10324 KB |
Output is correct |
32 |
Correct |
12 ms |
10376 KB |
Output is correct |
33 |
Correct |
11 ms |
10504 KB |
Output is correct |
34 |
Correct |
10 ms |
10316 KB |
Output is correct |
35 |
Correct |
10 ms |
10316 KB |
Output is correct |
36 |
Correct |
11 ms |
10316 KB |
Output is correct |
37 |
Correct |
10 ms |
10244 KB |
Output is correct |
38 |
Correct |
11 ms |
10368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9720 KB |
Output is correct |
2 |
Correct |
7 ms |
9716 KB |
Output is correct |
3 |
Correct |
7 ms |
9716 KB |
Output is correct |
4 |
Correct |
8 ms |
9716 KB |
Output is correct |
5 |
Correct |
7 ms |
9676 KB |
Output is correct |
6 |
Correct |
7 ms |
9736 KB |
Output is correct |
7 |
Correct |
7 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9716 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
7 ms |
9720 KB |
Output is correct |
12 |
Correct |
7 ms |
9676 KB |
Output is correct |
13 |
Correct |
7 ms |
9676 KB |
Output is correct |
14 |
Correct |
7 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
7 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9716 KB |
Output is correct |
19 |
Correct |
6 ms |
9724 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9676 KB |
Output is correct |
22 |
Correct |
13 ms |
10308 KB |
Output is correct |
23 |
Correct |
11 ms |
10316 KB |
Output is correct |
24 |
Correct |
11 ms |
10320 KB |
Output is correct |
25 |
Correct |
11 ms |
10316 KB |
Output is correct |
26 |
Correct |
12 ms |
10312 KB |
Output is correct |
27 |
Correct |
11 ms |
10316 KB |
Output is correct |
28 |
Correct |
11 ms |
10296 KB |
Output is correct |
29 |
Correct |
11 ms |
10240 KB |
Output is correct |
30 |
Correct |
11 ms |
10316 KB |
Output is correct |
31 |
Correct |
11 ms |
10324 KB |
Output is correct |
32 |
Correct |
12 ms |
10376 KB |
Output is correct |
33 |
Correct |
11 ms |
10504 KB |
Output is correct |
34 |
Correct |
10 ms |
10316 KB |
Output is correct |
35 |
Correct |
10 ms |
10316 KB |
Output is correct |
36 |
Correct |
11 ms |
10316 KB |
Output is correct |
37 |
Correct |
10 ms |
10244 KB |
Output is correct |
38 |
Correct |
11 ms |
10368 KB |
Output is correct |
39 |
Correct |
402 ms |
38608 KB |
Output is correct |
40 |
Correct |
395 ms |
37720 KB |
Output is correct |
41 |
Correct |
374 ms |
38784 KB |
Output is correct |
42 |
Correct |
385 ms |
39148 KB |
Output is correct |
43 |
Correct |
398 ms |
39096 KB |
Output is correct |
44 |
Correct |
399 ms |
39304 KB |
Output is correct |
45 |
Correct |
597 ms |
46164 KB |
Output is correct |
46 |
Correct |
434 ms |
50904 KB |
Output is correct |
47 |
Correct |
324 ms |
39504 KB |
Output is correct |
48 |
Correct |
249 ms |
39252 KB |
Output is correct |
49 |
Correct |
433 ms |
39832 KB |
Output is correct |
50 |
Correct |
289 ms |
39372 KB |
Output is correct |
51 |
Correct |
362 ms |
48820 KB |
Output is correct |