#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define mp make_pair
#define long int64_t
#define sz(x) int(std::size(x))
constexpr int maxn = 2e5 + 5;
bool vis[maxn];
int n, siz[maxn], depth[maxn], ans[maxn];
vector<int> graph[maxn], siz1[maxn];
void pdfs(int u, int p = -1) {
siz[u] = 1;
for (auto& v : graph[u]) {
if (v != p) {
pdfs(v, u);
siz[u] += siz[v];
siz1[u].push_back(siz[v]);
} else {
siz1[u].push_back(-1);
}
}
if (p != -1) {
*find(begin(siz1[u]), end(siz1[u]), -1) = n - siz[u];
}
}
void go(int u) {
struct DepthDfs {
void dfs(int u, int p = -1, int d = 0) {
depth[u] = d;
for (auto& v : graph[u]) {
if (!vis[v] && v != p) {
dfs(v, u, d + 1);
}
}
}
static void solve(int u) {
DepthDfs().dfs(u);
}
};
DepthDfs::solve(u);
struct Dfs {
vector<pair<int, int>> ch;
void dfs(int u, int p) {
for (int i = 0; i < sz(graph[u]); i++) {
int v = graph[u][i];
if (v == p) {
ch.emplace_back(n - siz1[u][i], depth[u]);
} else if (!vis[v]) {
dfs(v, u);
}
}
}
static vector<pair<int, int>> solve(int u, int p) {
Dfs d;
d.dfs(u, p);
return d.ch;
}
};
for (int _i = 0; _i < 2; _i++) {
map<int, int> copt;
auto query = [&](int x, int d) -> void {
auto it = copt.lower_bound(x);
if (it != copt.end()) {
ans[x] = max(ans[x], d + it->second);
}
};
auto update = [&](int x, int d) -> void {
auto it = copt.lower_bound(x);
if (it != copt.end() && it->second >= d) {
return;
}
while (it != copt.begin() && d >= (--it)->second) {
it = copt.erase(it);
}
copt[x] = d;
};
for (int i = 0; i < sz(graph[u]); i++) {
int v = graph[u][i], csz = n - siz1[u][i];
if (vis[v]) {
continue;
}
auto&& cv = Dfs::solve(v, u);
for (auto& [x, d] : cv) {
query(x, d);
ans[min(x, csz)] = max(ans[min(x, csz)], d);
}
for (auto& [x, d] : cv) {
update(x, d);
}
}
reverse(begin(graph[u]), end(graph[u]));
reverse(begin(siz1[u]), end(siz1[u]));
}
}
void dfs(int u) {
while (true) {
pair<int, int> mx {-1, -1};
for (auto& v : graph[u]) {
if (!vis[v]) {
mx = max(mx, mp(siz[v], v));
}
}
if (mx.first <= siz[u] / 2) {
break;
}
int v = mx.second;
siz[u] -= siz[v];
siz[v] += siz[u];
u = v;
}
go(u);
vis[u] = true;
for (auto& v : graph[u]) {
if (!vis[v]) {
dfs(v);
}
}
}
void solve() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
pdfs(0);
dfs(0);
for (int i = n; i >= 0; i--) {
ans[i] = max(ans[i], ans[i + 1]);
}
for (int i = 1; i <= n; i++) {
if (i & 1) {
cout << 1 << endl;
} else {
cout << ans[i / 2] + 1 << endl;
}
}
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9620 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9724 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9612 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9700 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9668 KB |
Output is correct |
21 |
Correct |
6 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9620 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9724 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9612 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9700 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9668 KB |
Output is correct |
21 |
Correct |
6 ms |
9728 KB |
Output is correct |
22 |
Correct |
14 ms |
10068 KB |
Output is correct |
23 |
Correct |
15 ms |
10124 KB |
Output is correct |
24 |
Correct |
11 ms |
10068 KB |
Output is correct |
25 |
Correct |
11 ms |
10124 KB |
Output is correct |
26 |
Correct |
11 ms |
10068 KB |
Output is correct |
27 |
Correct |
11 ms |
10068 KB |
Output is correct |
28 |
Correct |
12 ms |
10052 KB |
Output is correct |
29 |
Correct |
12 ms |
10052 KB |
Output is correct |
30 |
Correct |
11 ms |
10132 KB |
Output is correct |
31 |
Correct |
15 ms |
10068 KB |
Output is correct |
32 |
Correct |
21 ms |
10228 KB |
Output is correct |
33 |
Correct |
18 ms |
10448 KB |
Output is correct |
34 |
Correct |
9 ms |
9996 KB |
Output is correct |
35 |
Correct |
8 ms |
10068 KB |
Output is correct |
36 |
Correct |
12 ms |
10056 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
17 ms |
10196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9620 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
6 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9724 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9612 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9700 KB |
Output is correct |
16 |
Correct |
6 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9732 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9668 KB |
Output is correct |
21 |
Correct |
6 ms |
9728 KB |
Output is correct |
22 |
Correct |
14 ms |
10068 KB |
Output is correct |
23 |
Correct |
15 ms |
10124 KB |
Output is correct |
24 |
Correct |
11 ms |
10068 KB |
Output is correct |
25 |
Correct |
11 ms |
10124 KB |
Output is correct |
26 |
Correct |
11 ms |
10068 KB |
Output is correct |
27 |
Correct |
11 ms |
10068 KB |
Output is correct |
28 |
Correct |
12 ms |
10052 KB |
Output is correct |
29 |
Correct |
12 ms |
10052 KB |
Output is correct |
30 |
Correct |
11 ms |
10132 KB |
Output is correct |
31 |
Correct |
15 ms |
10068 KB |
Output is correct |
32 |
Correct |
21 ms |
10228 KB |
Output is correct |
33 |
Correct |
18 ms |
10448 KB |
Output is correct |
34 |
Correct |
9 ms |
9996 KB |
Output is correct |
35 |
Correct |
8 ms |
10068 KB |
Output is correct |
36 |
Correct |
12 ms |
10056 KB |
Output is correct |
37 |
Correct |
8 ms |
10068 KB |
Output is correct |
38 |
Correct |
17 ms |
10196 KB |
Output is correct |
39 |
Correct |
638 ms |
26724 KB |
Output is correct |
40 |
Correct |
624 ms |
26324 KB |
Output is correct |
41 |
Correct |
671 ms |
26652 KB |
Output is correct |
42 |
Correct |
688 ms |
26848 KB |
Output is correct |
43 |
Correct |
732 ms |
27068 KB |
Output is correct |
44 |
Correct |
764 ms |
26536 KB |
Output is correct |
45 |
Correct |
1496 ms |
35612 KB |
Output is correct |
46 |
Correct |
1534 ms |
44704 KB |
Output is correct |
47 |
Correct |
374 ms |
28708 KB |
Output is correct |
48 |
Correct |
291 ms |
29068 KB |
Output is correct |
49 |
Correct |
1036 ms |
30516 KB |
Output is correct |
50 |
Correct |
360 ms |
30604 KB |
Output is correct |
51 |
Correct |
942 ms |
41612 KB |
Output is correct |