#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define dbg(x) cerr << #x << " " << x << "\n"
const int MAX_N = 2e5;
int n;
vector <int> graph[1 + MAX_N];
int sz[1 + MAX_N];
int sol[1 + MAX_N];
bool used[1 + MAX_N];
int h[1 + MAX_N];
int best_depth[1 + MAX_N];
void upd (int &a, int b) {
if (a < b)
a = b;
}
void dfs_sz (int node, int par) {
sz[node] = 1;
for (int son : graph[node])
if (son != par && not used[son]) {
dfs_sz (son, node);
sz[node] += sz[son];
}
}
int get_centroid (int node, int par, int target) {
for (int son : graph[node])
if (son != par && not used[son] && sz[son] > target)
return get_centroid (son, node, target);
return node;
}
struct node_t {
int sz;
int h;
int id;
bool operator < (const node_t &other) const {
return sz < other.sz;
}
};
vector <node_t> v;
void dfs_prec (int node, int par, int depth, int nr) {
sz[node] = 1;
h[node] = depth;
for (int son : graph[node])
if (son != par && not used[son]) {
dfs_prec (son, node, depth + 1, nr);
sz[node] += sz[son];
}
v.push_back ({sz[node], h[node], nr});
}
void solve (int node) { /// centroid :)
dfs_sz (node, 0);
int centroid = get_centroid (node, 0, sz[node] / 2);
v.clear ();
int nr = 0;
for (int son : graph[centroid])
if (not used[son]) {
nr++;
dfs_prec (son, centroid, 1, nr);
}
for (int i = 0; i <= nr; i++)
best_depth[i] = 0;
best_depth[0] = 0;;
multiset <int> max_depths;
max_depths.insert (0);
sort (v.begin (), v.end ());
reverse (v.begin (), v.end ());
int min_sz = 0;
// dbg (centroid);
for (node_t node : v) {
// dbg (node.sz); dbg (node.h); dbg (node.id);
min_sz = node.sz;
if (best_depth[node.id] < node.h) {
if (best_depth[node.id] > 0)
max_depths.erase (max_depths.find (best_depth[node.id]));
max_depths.insert (node.h);
best_depth[node.id] = node.h;
}
if (max_depths.size () > 1) {
auto it = max_depths.end ();
it = prev (it);
int dist1 = *it;
it = prev (it);
int dist2 = *it;
// dbg (dist1); dbg (dist2);
sol[min_sz * 2] = max (sol[min_sz * 2], dist1 + dist2 + 1);
}
}
used[centroid] = true;
for (int vec : graph[centroid])
if (not used[vec])
solve (vec);
}
/**
5
1 2
2 3
4 2
3 5
**/
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n;
for (int i = 1; i < n; i++) {
register int x, y;
cin >> x >> y;
graph[x].push_back (y);
graph[y].push_back (x);
}
if (n == 2) {
cout << "1\n2\n";
return 0;
}
for (int i = 1; i <= n; i++)
sol[i] = 1;
solve (1);
for (int i = n - 2; i > 0; i--)
sol[i] = max (sol[i], sol[i + 2]);
for (int i = 1; i <= n; i++)
cout << sol[i] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
5 ms |
5020 KB |
Output is correct |
8 |
Correct |
5 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
5 ms |
5020 KB |
Output is correct |
8 |
Correct |
5 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
8 ms |
5368 KB |
Output is correct |
23 |
Correct |
8 ms |
5324 KB |
Output is correct |
24 |
Correct |
8 ms |
5324 KB |
Output is correct |
25 |
Correct |
9 ms |
5224 KB |
Output is correct |
26 |
Correct |
9 ms |
5324 KB |
Output is correct |
27 |
Correct |
8 ms |
5324 KB |
Output is correct |
28 |
Correct |
11 ms |
5324 KB |
Output is correct |
29 |
Correct |
8 ms |
5356 KB |
Output is correct |
30 |
Correct |
10 ms |
5324 KB |
Output is correct |
31 |
Correct |
8 ms |
5368 KB |
Output is correct |
32 |
Correct |
10 ms |
5416 KB |
Output is correct |
33 |
Correct |
11 ms |
5548 KB |
Output is correct |
34 |
Correct |
6 ms |
5324 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
8 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5452 KB |
Output is correct |
38 |
Correct |
8 ms |
5524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5020 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
5 ms |
5020 KB |
Output is correct |
8 |
Correct |
5 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
4 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
4 ms |
4940 KB |
Output is correct |
13 |
Correct |
4 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5024 KB |
Output is correct |
15 |
Correct |
4 ms |
4944 KB |
Output is correct |
16 |
Correct |
4 ms |
5020 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
8 ms |
5368 KB |
Output is correct |
23 |
Correct |
8 ms |
5324 KB |
Output is correct |
24 |
Correct |
8 ms |
5324 KB |
Output is correct |
25 |
Correct |
9 ms |
5224 KB |
Output is correct |
26 |
Correct |
9 ms |
5324 KB |
Output is correct |
27 |
Correct |
8 ms |
5324 KB |
Output is correct |
28 |
Correct |
11 ms |
5324 KB |
Output is correct |
29 |
Correct |
8 ms |
5356 KB |
Output is correct |
30 |
Correct |
10 ms |
5324 KB |
Output is correct |
31 |
Correct |
8 ms |
5368 KB |
Output is correct |
32 |
Correct |
10 ms |
5416 KB |
Output is correct |
33 |
Correct |
11 ms |
5548 KB |
Output is correct |
34 |
Correct |
6 ms |
5324 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
8 ms |
5324 KB |
Output is correct |
37 |
Correct |
6 ms |
5452 KB |
Output is correct |
38 |
Correct |
8 ms |
5524 KB |
Output is correct |
39 |
Correct |
649 ms |
19356 KB |
Output is correct |
40 |
Correct |
634 ms |
19352 KB |
Output is correct |
41 |
Correct |
508 ms |
19304 KB |
Output is correct |
42 |
Correct |
611 ms |
19524 KB |
Output is correct |
43 |
Correct |
513 ms |
19472 KB |
Output is correct |
44 |
Correct |
508 ms |
19512 KB |
Output is correct |
45 |
Correct |
1067 ms |
25072 KB |
Output is correct |
46 |
Correct |
1000 ms |
30548 KB |
Output is correct |
47 |
Correct |
250 ms |
26216 KB |
Output is correct |
48 |
Correct |
187 ms |
29832 KB |
Output is correct |
49 |
Correct |
629 ms |
20024 KB |
Output is correct |
50 |
Correct |
298 ms |
24880 KB |
Output is correct |
51 |
Correct |
506 ms |
32300 KB |
Output is correct |