#include <bits/stdc++.h>
#define vi vector <int>
#define pb push_back
using namespace std;
struct LCA{
vi st, ed, _lg, high;
vector <vi> euler, adj;
int cnt;
void dfs(int u, int pa){
++ cnt;
st[u] = cnt;
euler[cnt].pb(u);
for (int v : adj[u]){
if (v == pa) continue;
high[v] = high[u] + 1;
dfs(v, u);
euler[++ cnt].pb(u);
}
ed[u] = cnt;
}
int check(int u, int v) {
return st[u] <= st[v] && ed[v] <= ed[u];
}
int min_by_time(int u, int v) {
return (st[u] < st[v] ? u : v);
}
void add(int u, int v){
adj[u].pb(v);
adj[v].pb(u);
}
LCA(int n){
st.resize(n + 3, 0);
ed.resize(n + 3, 0);
euler.resize(3 * n + 5);
adj.resize(n + 3);
_lg.resize(3 * n + 5);
high.resize(n + 3, 1);
for (int i = 0; i <= 3 * n; ++ i) _lg[i] = log2(i);
}
void init(int r){
cnt = 0;
dfs(r, 0);
for (int lg = 1; lg < 20; ++lg) {
for (int i = 1; i + (1 << lg) - 1 <= cnt; ++i)
euler[i].pb(min_by_time(euler[i][lg - 1], euler[i + (1 << lg - 1)][lg - 1]));
}
}
int get(int u, int v) {
int L = min(st[u], st[v]);
int R = max(ed[u], ed[v]);
int lg = _lg[R - L + 1];
return min_by_time(euler[L][lg], euler[R - (1 << lg) + 1][lg]);
}
int dist(int u, int v){
int lc = get(u, v);
return high[u] + high[v] - 2 * high[lc];
}
};
const int N = 2e5 + 5;
int n, sz[N];
vector <int> adj[N];
void dfs(int u, int p = -1){
sz[u] = 1;
for (int v : adj[u]){
if (v == p) continue;
dfs(v, u); sz[u] += sz[v];
}
}
int find_cen(int u, int p = -1){
for (int v : adj[u]){
if (v == p) continue;
if (sz[v] * 2 > n) return find_cen(v, u);
}
return u;
}
vector <int> cur[N / 2];
int ans[N];
void solve(){
cin >> n;
LCA tree(n);
for (int i = 1, u, v; i < n; ++ i){
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
tree.add(u, v);
}
dfs(1);
int r = find_cen(1);
tree.init(r); dfs(r);
for (int i = 1; i <= n; ++ i) {
if (i != r) cur[sz[i]].push_back(i);
if (i & 1) ans[i] = 1;
}
int u = r, v = r, Max = 0;
for (int i = n / 2; i >= 1; -- i){
for (int k : cur[i]){
if (tree.dist(u, k) < tree.dist(v, k)) swap(u, v);
if (tree.dist(u, k) > Max){
Max = tree.dist(u, k);
v = k;
}
}
ans[i * 2] = Max + 1;
}
for (int i = 1; i <= n; ++ i) cout << ans[i] << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
Compilation message
meetings2.cpp: In member function 'void LCA::init(int)':
meetings2.cpp:53:78: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | euler[i].pb(min_by_time(euler[i][lg - 1], euler[i + (1 << lg - 1)][lg - 1]));
| ~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7512 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7524 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7460 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7408 KB |
Output is correct |
19 |
Correct |
3 ms |
7520 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7512 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7524 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7460 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7408 KB |
Output is correct |
19 |
Correct |
3 ms |
7520 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
9 ms |
9308 KB |
Output is correct |
23 |
Correct |
7 ms |
8796 KB |
Output is correct |
24 |
Correct |
6 ms |
8892 KB |
Output is correct |
25 |
Correct |
9 ms |
8796 KB |
Output is correct |
26 |
Correct |
7 ms |
9052 KB |
Output is correct |
27 |
Correct |
7 ms |
8828 KB |
Output is correct |
28 |
Correct |
7 ms |
9048 KB |
Output is correct |
29 |
Correct |
6 ms |
8808 KB |
Output is correct |
30 |
Correct |
6 ms |
8792 KB |
Output is correct |
31 |
Correct |
6 ms |
8928 KB |
Output is correct |
32 |
Correct |
6 ms |
9052 KB |
Output is correct |
33 |
Correct |
6 ms |
9052 KB |
Output is correct |
34 |
Correct |
7 ms |
8796 KB |
Output is correct |
35 |
Correct |
6 ms |
8936 KB |
Output is correct |
36 |
Correct |
6 ms |
8796 KB |
Output is correct |
37 |
Correct |
8 ms |
8796 KB |
Output is correct |
38 |
Correct |
6 ms |
9052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7512 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
4 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7524 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
3 ms |
7516 KB |
Output is correct |
7 |
Correct |
3 ms |
7516 KB |
Output is correct |
8 |
Correct |
4 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7512 KB |
Output is correct |
10 |
Correct |
3 ms |
7512 KB |
Output is correct |
11 |
Correct |
3 ms |
7516 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
3 ms |
7524 KB |
Output is correct |
14 |
Correct |
3 ms |
7516 KB |
Output is correct |
15 |
Correct |
3 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7460 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
3 ms |
7408 KB |
Output is correct |
19 |
Correct |
3 ms |
7520 KB |
Output is correct |
20 |
Correct |
3 ms |
7512 KB |
Output is correct |
21 |
Correct |
3 ms |
7516 KB |
Output is correct |
22 |
Correct |
9 ms |
9308 KB |
Output is correct |
23 |
Correct |
7 ms |
8796 KB |
Output is correct |
24 |
Correct |
6 ms |
8892 KB |
Output is correct |
25 |
Correct |
9 ms |
8796 KB |
Output is correct |
26 |
Correct |
7 ms |
9052 KB |
Output is correct |
27 |
Correct |
7 ms |
8828 KB |
Output is correct |
28 |
Correct |
7 ms |
9048 KB |
Output is correct |
29 |
Correct |
6 ms |
8808 KB |
Output is correct |
30 |
Correct |
6 ms |
8792 KB |
Output is correct |
31 |
Correct |
6 ms |
8928 KB |
Output is correct |
32 |
Correct |
6 ms |
9052 KB |
Output is correct |
33 |
Correct |
6 ms |
9052 KB |
Output is correct |
34 |
Correct |
7 ms |
8796 KB |
Output is correct |
35 |
Correct |
6 ms |
8936 KB |
Output is correct |
36 |
Correct |
6 ms |
8796 KB |
Output is correct |
37 |
Correct |
8 ms |
8796 KB |
Output is correct |
38 |
Correct |
6 ms |
9052 KB |
Output is correct |
39 |
Correct |
332 ms |
99696 KB |
Output is correct |
40 |
Correct |
332 ms |
97488 KB |
Output is correct |
41 |
Correct |
333 ms |
100124 KB |
Output is correct |
42 |
Correct |
335 ms |
101832 KB |
Output is correct |
43 |
Correct |
321 ms |
101576 KB |
Output is correct |
44 |
Correct |
331 ms |
101616 KB |
Output is correct |
45 |
Correct |
362 ms |
107976 KB |
Output is correct |
46 |
Correct |
370 ms |
112532 KB |
Output is correct |
47 |
Correct |
296 ms |
102364 KB |
Output is correct |
48 |
Correct |
267 ms |
102572 KB |
Output is correct |
49 |
Correct |
288 ms |
102604 KB |
Output is correct |
50 |
Correct |
272 ms |
102732 KB |
Output is correct |
51 |
Correct |
305 ms |
110676 KB |
Output is correct |