#include<bits/stdc++.h>
using namespace std;
#define fori(i, l, r) for(int i = (int) (l); i <= (int) (r); i++)
#define ford(i, r, l) for(int i = (int) (r); i >= (int) (l); i--)
#define ii pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define eb emplace_back
#define em emplace
#define sp ' '
#define endl '\n'
#define int long long
const int maxn =2e5 +5;
int n;
vi g[maxn];
int tim;
int tin[maxn];
int dep[maxn];
int sz[maxn];
int euler[maxn * 2];
int lca[21][maxn * 2];
vector<int> nodes_sz[maxn];
pair<int, ii> Dia;
int ans[maxn];
void dfs_sz(int u, int p) {
sz[u] = 1;
for(int v: g[u]) {
if(v - p) {
dfs_sz(v, u);
sz[u] += sz[v];
}
}
}
int get_cen(int u, int p) {
for(int v: g[u]) {
if(v - p and sz[v] * 2 > sz[1]) return get_cen(v, u);
}
return u;
}
void dfs_prep(int u, int p) {
sz[u] = 1;
++tim;
tin[u] = tim;
euler[tim] = u;
for(int v: g[u]) {
if(v - p) {
dep[v] = dep[u] + 1;
dfs_prep(v, u);
sz[u] += sz[v];
++tim;
euler[tim] = u;
}
}
}
int hg(int u, int v) {
return dep[u] < dep[v]? u: v;
}
int get_lca(int u, int v) {
int l = tin[u], r = tin[v];
if(r < l) swap(r, l);
int bit = __lg(r - l + 1);
return hg(lca[bit][l], lca[bit][r - (1 << bit) + 1]);
}
int get_dis(int u, int v) {
return dep[u] + dep[v] - 2 * dep[get_lca(u, v)];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
fori(i, 2, n) {
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
dfs_sz(1, 1);
int cen = get_cen(1, 1);
dfs_prep(cen, cen);
// cout << cen << endl;
fori(bit, 0, 20) {
fori(i, 1, tim - (1 << bit) + 1) {
lca[bit][i] = !bit? euler[i]: hg(lca[bit - 1][i], lca[bit - 1][i + (1 << bit - 1)]);
}
}
// cout << get_dis(1, 5) << endl;
fori(i, 1, n) nodes_sz[sz[i]].eb(i);
Dia = make_pair(0, ii(cen, cen));
int pt = n;
ford(j, n, 1) {
if(j & 1) ans[j] = 1;
else {
for(; pt >= j / 2; pt--) {
for(int u: nodes_sz[pt]) {
// cout << pt << ": " << u << endl;
int l = Dia.se.fi, r = Dia.se.se;
Dia = max({Dia, make_pair(get_dis(l, u), ii(l, u)), make_pair(get_dis(u, r), ii(u, r))});
}
}
ans[j] = 1 + Dia.fi;
}
}
fori(i, 1, n) cout << ans[i] << endl;
}
Compilation message
meetings2.cpp: In function 'int main()':
meetings2.cpp:95:81: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
95 | lca[bit][i] = !bit? euler[i]: hg(lca[bit - 1][i], lca[bit - 1][i + (1 << bit - 1)]);
| ~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9760 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9696 KB |
Output is correct |
10 |
Correct |
7 ms |
9696 KB |
Output is correct |
11 |
Correct |
6 ms |
9736 KB |
Output is correct |
12 |
Correct |
5 ms |
9732 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9760 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9696 KB |
Output is correct |
10 |
Correct |
7 ms |
9696 KB |
Output is correct |
11 |
Correct |
6 ms |
9736 KB |
Output is correct |
12 |
Correct |
5 ms |
9732 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
9 ms |
11044 KB |
Output is correct |
23 |
Correct |
10 ms |
10928 KB |
Output is correct |
24 |
Correct |
8 ms |
11028 KB |
Output is correct |
25 |
Correct |
8 ms |
10964 KB |
Output is correct |
26 |
Correct |
8 ms |
10964 KB |
Output is correct |
27 |
Correct |
8 ms |
10964 KB |
Output is correct |
28 |
Correct |
9 ms |
10964 KB |
Output is correct |
29 |
Correct |
10 ms |
11032 KB |
Output is correct |
30 |
Correct |
7 ms |
11024 KB |
Output is correct |
31 |
Correct |
8 ms |
10964 KB |
Output is correct |
32 |
Correct |
8 ms |
11084 KB |
Output is correct |
33 |
Correct |
7 ms |
11092 KB |
Output is correct |
34 |
Correct |
7 ms |
11028 KB |
Output is correct |
35 |
Correct |
8 ms |
10964 KB |
Output is correct |
36 |
Correct |
8 ms |
11020 KB |
Output is correct |
37 |
Correct |
7 ms |
11028 KB |
Output is correct |
38 |
Correct |
8 ms |
11092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9760 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
6 ms |
9684 KB |
Output is correct |
9 |
Correct |
7 ms |
9696 KB |
Output is correct |
10 |
Correct |
7 ms |
9696 KB |
Output is correct |
11 |
Correct |
6 ms |
9736 KB |
Output is correct |
12 |
Correct |
5 ms |
9732 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9684 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
5 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
22 |
Correct |
9 ms |
11044 KB |
Output is correct |
23 |
Correct |
10 ms |
10928 KB |
Output is correct |
24 |
Correct |
8 ms |
11028 KB |
Output is correct |
25 |
Correct |
8 ms |
10964 KB |
Output is correct |
26 |
Correct |
8 ms |
10964 KB |
Output is correct |
27 |
Correct |
8 ms |
10964 KB |
Output is correct |
28 |
Correct |
9 ms |
10964 KB |
Output is correct |
29 |
Correct |
10 ms |
11032 KB |
Output is correct |
30 |
Correct |
7 ms |
11024 KB |
Output is correct |
31 |
Correct |
8 ms |
10964 KB |
Output is correct |
32 |
Correct |
8 ms |
11084 KB |
Output is correct |
33 |
Correct |
7 ms |
11092 KB |
Output is correct |
34 |
Correct |
7 ms |
11028 KB |
Output is correct |
35 |
Correct |
8 ms |
10964 KB |
Output is correct |
36 |
Correct |
8 ms |
11020 KB |
Output is correct |
37 |
Correct |
7 ms |
11028 KB |
Output is correct |
38 |
Correct |
8 ms |
11092 KB |
Output is correct |
39 |
Correct |
195 ms |
85376 KB |
Output is correct |
40 |
Correct |
201 ms |
83516 KB |
Output is correct |
41 |
Correct |
225 ms |
85768 KB |
Output is correct |
42 |
Correct |
211 ms |
86948 KB |
Output is correct |
43 |
Correct |
191 ms |
86908 KB |
Output is correct |
44 |
Correct |
251 ms |
86992 KB |
Output is correct |
45 |
Correct |
209 ms |
91840 KB |
Output is correct |
46 |
Correct |
213 ms |
94284 KB |
Output is correct |
47 |
Correct |
196 ms |
87032 KB |
Output is correct |
48 |
Correct |
129 ms |
86836 KB |
Output is correct |
49 |
Correct |
177 ms |
87252 KB |
Output is correct |
50 |
Correct |
152 ms |
86988 KB |
Output is correct |
51 |
Correct |
175 ms |
93316 KB |
Output is correct |