#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, LG = 18;
int n;
int res[N], pr[LG][N], sz[N], dep[N];
vector<int> g[N];
void dfs(int u) {
sz[u] = 1;
for (int v : g[u]) {
if (v != pr[0][u]) {
pr[0][v] = u;
dep[v] = dep[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
}
int findCen(int u, int p) {
for (int v : g[u]) {
if (v != p && sz[v] * 2 > n) {
return findCen(v, u);
}
}
return u;
}
int lca(int u, int v) {
if (dep[u] < dep[v]) {
swap(u, v);
}
for (int j = LG - 1; ~j; --j) {
if (dep[u] - (1 << j) >= dep[v]) {
u = pr[j][u];
}
}
if (u == v) {
return u;
}
for (int j = LG - 1; ~j; --j) {
if (pr[j][u] ^ pr[j][v]) {
u = pr[j][u], v = pr[j][v];
}
}
return pr[0][u];
}
int dis(int u, int v) {
return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1);
int c = findCen(1, 1);
pr[0][c] = 0;
dfs(c);
for (int j = 1; j < LG; ++j) {
for (int i = 1; i <= n; ++i) {
pr[j][i] = pr[j - 1][pr[j - 1][i]];
}
}
array<int, 3> best{0, c, c};
vector<int> cands;
for (int i = 1; i <= n; ++i) {
if (i ^ c) {
cands.push_back(i);
}
}
sort(cands.begin(), cands.end(), [&](int i, int j) {
return sz[i] > sz[j];
});
for (int i = n / 2, j = 0; i > 0; --i) {
while (j < cands.size() && sz[cands[j]] == i) {
int u = cands[j], a = best[1], b = best[2];
best = max({best, {dis(u, a), a, u}, {dis(u, b), b, u}});
++j;
}
res[i * 2] = best[0];
}
for (int i = 1; i <= n; ++i) {
cout << res[i] + 1 << "\n";
}
return 0;
}
Compilation message
meetings2.cpp: In function 'int main()':
meetings2.cpp:91:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | while (j < cands.size() && sz[cands[j]] == i) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21160 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21132 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
3 ms |
21084 KB |
Output is correct |
9 |
Correct |
2 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21080 KB |
Output is correct |
15 |
Correct |
2 ms |
21084 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
2 ms |
21084 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21160 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21132 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
3 ms |
21084 KB |
Output is correct |
9 |
Correct |
2 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21080 KB |
Output is correct |
15 |
Correct |
2 ms |
21084 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
2 ms |
21084 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21592 KB |
Output is correct |
23 |
Correct |
4 ms |
21340 KB |
Output is correct |
24 |
Correct |
4 ms |
21268 KB |
Output is correct |
25 |
Correct |
4 ms |
21232 KB |
Output is correct |
26 |
Correct |
5 ms |
21340 KB |
Output is correct |
27 |
Correct |
4 ms |
21340 KB |
Output is correct |
28 |
Correct |
4 ms |
21340 KB |
Output is correct |
29 |
Correct |
4 ms |
21340 KB |
Output is correct |
30 |
Correct |
4 ms |
21340 KB |
Output is correct |
31 |
Correct |
5 ms |
21592 KB |
Output is correct |
32 |
Correct |
4 ms |
21340 KB |
Output is correct |
33 |
Correct |
5 ms |
21340 KB |
Output is correct |
34 |
Correct |
4 ms |
21372 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
4 ms |
21340 KB |
Output is correct |
37 |
Correct |
3 ms |
21336 KB |
Output is correct |
38 |
Correct |
4 ms |
21400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
21084 KB |
Output is correct |
2 |
Correct |
3 ms |
21160 KB |
Output is correct |
3 |
Correct |
2 ms |
21084 KB |
Output is correct |
4 |
Correct |
2 ms |
21084 KB |
Output is correct |
5 |
Correct |
2 ms |
21132 KB |
Output is correct |
6 |
Correct |
2 ms |
21084 KB |
Output is correct |
7 |
Correct |
2 ms |
21084 KB |
Output is correct |
8 |
Correct |
3 ms |
21084 KB |
Output is correct |
9 |
Correct |
2 ms |
21084 KB |
Output is correct |
10 |
Correct |
3 ms |
21084 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
2 ms |
21084 KB |
Output is correct |
14 |
Correct |
2 ms |
21080 KB |
Output is correct |
15 |
Correct |
2 ms |
21084 KB |
Output is correct |
16 |
Correct |
2 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
2 ms |
21084 KB |
Output is correct |
19 |
Correct |
2 ms |
21084 KB |
Output is correct |
20 |
Correct |
2 ms |
21084 KB |
Output is correct |
21 |
Correct |
3 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21592 KB |
Output is correct |
23 |
Correct |
4 ms |
21340 KB |
Output is correct |
24 |
Correct |
4 ms |
21268 KB |
Output is correct |
25 |
Correct |
4 ms |
21232 KB |
Output is correct |
26 |
Correct |
5 ms |
21340 KB |
Output is correct |
27 |
Correct |
4 ms |
21340 KB |
Output is correct |
28 |
Correct |
4 ms |
21340 KB |
Output is correct |
29 |
Correct |
4 ms |
21340 KB |
Output is correct |
30 |
Correct |
4 ms |
21340 KB |
Output is correct |
31 |
Correct |
5 ms |
21592 KB |
Output is correct |
32 |
Correct |
4 ms |
21340 KB |
Output is correct |
33 |
Correct |
5 ms |
21340 KB |
Output is correct |
34 |
Correct |
4 ms |
21372 KB |
Output is correct |
35 |
Correct |
3 ms |
21340 KB |
Output is correct |
36 |
Correct |
4 ms |
21340 KB |
Output is correct |
37 |
Correct |
3 ms |
21336 KB |
Output is correct |
38 |
Correct |
4 ms |
21400 KB |
Output is correct |
39 |
Correct |
91 ms |
31556 KB |
Output is correct |
40 |
Correct |
113 ms |
31436 KB |
Output is correct |
41 |
Correct |
93 ms |
31688 KB |
Output is correct |
42 |
Correct |
91 ms |
31696 KB |
Output is correct |
43 |
Correct |
97 ms |
31780 KB |
Output is correct |
44 |
Correct |
93 ms |
31692 KB |
Output is correct |
45 |
Correct |
171 ms |
36548 KB |
Output is correct |
46 |
Correct |
119 ms |
40824 KB |
Output is correct |
47 |
Correct |
78 ms |
32132 KB |
Output is correct |
48 |
Correct |
87 ms |
32300 KB |
Output is correct |
49 |
Correct |
123 ms |
32484 KB |
Output is correct |
50 |
Correct |
73 ms |
32328 KB |
Output is correct |
51 |
Correct |
96 ms |
39748 KB |
Output is correct |