#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 2e5 + 5;
int n;
int sz[N];
int ans[N];
bool blocked[N];
vector<int> g[N];
void find_sizes(int v, int p) {
sz[v] = 1;
for (int u : g[v]) {
if (!blocked[u] && u != p) {
find_sizes(u, v);
sz[v] += sz[u];
}
}
}
int find_centroid(int v, int p, int x) {
for (int u : g[v]) {
if (!blocked[u] && u != p && sz[u] > x / 2) {
return find_centroid(u, v, x);
}
}
return v;
}
void dfs(int v, int p, int dep, vector<pair<int, int>>& vec) {
vec.emplace_back(sz[v], dep);
for (int u : g[v]) {
if (!blocked[u] && u != p) {
dfs(u, v, dep + 1, vec);
}
}
}
void merge(set<pair<int, int>>& se, vector<pair<int, int>>& vec) {
for (auto [sub, dep] : vec) {
auto it = se.lower_bound(make_pair(sub, -1));
if (it == se.end()) {
se.emplace(sub, dep);
} else {
if (it->second >= dep) {
continue;
}
it = se.insert({sub, dep}).first;
vector<pair<int, int>> to_erase;
while (it != se.begin()) {
it = prev(it);
if (it->second < dep) {
to_erase.push_back(*it);
} else {
break;
}
}
for (auto p : to_erase) {
se.erase(p);
}
}
}
}
void decompose(int v) {
find_sizes(v, v);
int centroid = find_centroid(v, v, sz[v]);
find_sizes(centroid, centroid);
blocked[centroid] = true;
for (int rep = 0; rep < 2; ++rep) {
set<pair<int, int>> se;
for (int u : g[centroid]) {
if (blocked[u]) {
continue;
}
vector<pair<int, int>> vec;
dfs(u, u, 1, vec);
for (int i = 0; i < (int) vec.size(); ++i) {
auto [sub, dep] = vec[i];
auto it = se.lower_bound(make_pair(sub, -1));
if (it != se.end()) {
ans[sub] = max(ans[sub], dep + it->second);
}
//if (sub < sz[centroid] - sub) {//always true
ans[sub] = max(ans[sub], dep);
//}
}
merge(se, vec);
}
reverse(g[centroid].begin(), g[centroid].end());
}
for (int u : g[centroid]) {
if (!blocked[u]) {
decompose(u);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
decompose(1);
for (int i = n; i >= 1; --i) {
ans[i] = max(ans[i], ans[i + 1]);
}
for (int i = 1; i <= n; ++i) {
cout << (i % 2 ? 1 : ans[i / 2] + 1) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6544 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6772 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6544 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6772 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
9 ms |
7004 KB |
Output is correct |
23 |
Correct |
5 ms |
7000 KB |
Output is correct |
24 |
Correct |
6 ms |
7004 KB |
Output is correct |
25 |
Correct |
5 ms |
7000 KB |
Output is correct |
26 |
Correct |
6 ms |
7004 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
5 ms |
7004 KB |
Output is correct |
29 |
Correct |
5 ms |
7004 KB |
Output is correct |
30 |
Correct |
5 ms |
7004 KB |
Output is correct |
31 |
Correct |
6 ms |
7004 KB |
Output is correct |
32 |
Correct |
10 ms |
7004 KB |
Output is correct |
33 |
Correct |
10 ms |
7260 KB |
Output is correct |
34 |
Correct |
3 ms |
6748 KB |
Output is correct |
35 |
Correct |
4 ms |
6748 KB |
Output is correct |
36 |
Correct |
6 ms |
7004 KB |
Output is correct |
37 |
Correct |
3 ms |
6892 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6744 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6544 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6772 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6748 KB |
Output is correct |
19 |
Correct |
1 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
9 ms |
7004 KB |
Output is correct |
23 |
Correct |
5 ms |
7000 KB |
Output is correct |
24 |
Correct |
6 ms |
7004 KB |
Output is correct |
25 |
Correct |
5 ms |
7000 KB |
Output is correct |
26 |
Correct |
6 ms |
7004 KB |
Output is correct |
27 |
Correct |
5 ms |
6748 KB |
Output is correct |
28 |
Correct |
5 ms |
7004 KB |
Output is correct |
29 |
Correct |
5 ms |
7004 KB |
Output is correct |
30 |
Correct |
5 ms |
7004 KB |
Output is correct |
31 |
Correct |
6 ms |
7004 KB |
Output is correct |
32 |
Correct |
10 ms |
7004 KB |
Output is correct |
33 |
Correct |
10 ms |
7260 KB |
Output is correct |
34 |
Correct |
3 ms |
6748 KB |
Output is correct |
35 |
Correct |
4 ms |
6748 KB |
Output is correct |
36 |
Correct |
6 ms |
7004 KB |
Output is correct |
37 |
Correct |
3 ms |
6892 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
39 |
Correct |
356 ms |
17240 KB |
Output is correct |
40 |
Correct |
334 ms |
17188 KB |
Output is correct |
41 |
Correct |
342 ms |
17448 KB |
Output is correct |
42 |
Correct |
329 ms |
17460 KB |
Output is correct |
43 |
Correct |
403 ms |
17448 KB |
Output is correct |
44 |
Correct |
381 ms |
17544 KB |
Output is correct |
45 |
Correct |
1006 ms |
24620 KB |
Output is correct |
46 |
Correct |
1042 ms |
31504 KB |
Output is correct |
47 |
Correct |
123 ms |
16552 KB |
Output is correct |
48 |
Correct |
80 ms |
16840 KB |
Output is correct |
49 |
Correct |
433 ms |
18144 KB |
Output is correct |
50 |
Correct |
114 ms |
18100 KB |
Output is correct |
51 |
Correct |
533 ms |
26832 KB |
Output is correct |