#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 500;
const ll INF = 1e18;
int n;
vector<int> susjedi[MAXN], v;
int rj[2 * MAXN], bio[MAXN], d[MAXN], w[MAXN], flag[MAXN];
void dfs (int x, int p) {
w[x] = 1;
v.push_back (x);
for (auto nx: susjedi[x]) {
if (nx == p || bio[nx]) continue;
d[nx] = d[x] + 1;
dfs (nx, x);
w[x] += w[nx];
}
}
void postavi_flag (int x, int p, int r) {
flag[x] = r;
for (auto nx: susjedi[x]) {
if (nx == p || bio[nx]) continue;
postavi_flag(nx, x, r);
}
}
bool cmp (int x, int y) {
return w[x] > w[y];
}
void centroid (int x) {
v.clear ();
d[x] = 0;
dfs (x, x);
for (auto it: v) {
bool dobro = true;
for (auto nx: susjedi[it]) {
if (bio[nx]) continue;
if (d[it] < d[nx] && w[nx] > v.size () / 2) dobro = false;
if (d[nx] < d[it] && v.size () - w[it] > v.size () / 2) dobro = false;
}
if (dobro) x = it;
}
v.clear ();
d[x] = 0;
dfs (x, x);
for (auto it: susjedi[x]) {
if (bio[it]) continue;
postavi_flag (it, x, it);
}
for (auto it: v) {
if (it == x) continue;
int ind = min (w[it], (int) v.size () - w[flag[it]]);
rj[ind] = max (rj[ind], d[it] + 1);
}
v.erase (find (v.begin (), v.end (), x));
sort (v.begin (), v.end (), cmp);
int prvi = -1, drugi = -1;
for (auto it: v) {
if (prvi == -1) {
prvi = it;
continue;
}
else if (drugi == -1 && flag[it] != flag[prvi]) drugi = it;
else if (drugi == -1 && flag[it] == flag[prvi] && d[it] > d[prvi]) {
prvi = it;
continue;
}
else if (drugi == -1 && flag[it] == flag[prvi]) continue;
else if (d[it] > d[prvi] && flag[it] != flag[prvi]) {
drugi = prvi;
prvi = it;
}
else if (d[it] > d[prvi] && flag[it] == flag[prvi]) prvi = it;
else if (d[it] > d[drugi] && flag[it] != flag[prvi]) drugi = it;
int ind = min (w[prvi], w[drugi]);
rj[ind] = max (rj[ind], d[prvi] + d[drugi] + 1);
}
bio[x] = 1;
for (auto it: susjedi[x]) {
if (!bio[it]) centroid (it);
}
}
int main () {
ios_base::sync_with_stdio (false);
cin.tie (0);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;b--;
susjedi[b].push_back (a);
susjedi[a].push_back (b);
}
centroid (0);
for (int i = n; i > 0; i--) rj[i] = max (max (rj[i], rj[i + 1]), 1);
for (int i = n; i > 0; i--) rj[2 * i] = rj[i];
for (int i = 1; i <= n; i+= 2) rj[i] = 1;
for (int i = 1; i <= n; i++) cout << rj[i] << "\n";
return 0;
}
Compilation message
meetings2.cpp: In function 'void centroid(int)':
meetings2.cpp:46:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | if (d[it] < d[nx] && w[nx] > v.size () / 2) dobro = false;
| ~~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
7512 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
7512 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7772 KB |
Output is correct |
23 |
Correct |
5 ms |
7768 KB |
Output is correct |
24 |
Correct |
5 ms |
7772 KB |
Output is correct |
25 |
Correct |
5 ms |
7772 KB |
Output is correct |
26 |
Correct |
6 ms |
7904 KB |
Output is correct |
27 |
Correct |
5 ms |
7772 KB |
Output is correct |
28 |
Correct |
5 ms |
7776 KB |
Output is correct |
29 |
Correct |
5 ms |
7904 KB |
Output is correct |
30 |
Correct |
5 ms |
7900 KB |
Output is correct |
31 |
Correct |
5 ms |
7772 KB |
Output is correct |
32 |
Correct |
6 ms |
8028 KB |
Output is correct |
33 |
Correct |
6 ms |
8280 KB |
Output is correct |
34 |
Correct |
3 ms |
7772 KB |
Output is correct |
35 |
Correct |
3 ms |
7752 KB |
Output is correct |
36 |
Correct |
5 ms |
7772 KB |
Output is correct |
37 |
Correct |
3 ms |
7768 KB |
Output is correct |
38 |
Correct |
4 ms |
8024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9560 KB |
Output is correct |
2 |
Correct |
2 ms |
7512 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
2 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
2 ms |
7516 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
2 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7516 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7516 KB |
Output is correct |
19 |
Correct |
2 ms |
7516 KB |
Output is correct |
20 |
Correct |
2 ms |
7516 KB |
Output is correct |
21 |
Correct |
2 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7772 KB |
Output is correct |
23 |
Correct |
5 ms |
7768 KB |
Output is correct |
24 |
Correct |
5 ms |
7772 KB |
Output is correct |
25 |
Correct |
5 ms |
7772 KB |
Output is correct |
26 |
Correct |
6 ms |
7904 KB |
Output is correct |
27 |
Correct |
5 ms |
7772 KB |
Output is correct |
28 |
Correct |
5 ms |
7776 KB |
Output is correct |
29 |
Correct |
5 ms |
7904 KB |
Output is correct |
30 |
Correct |
5 ms |
7900 KB |
Output is correct |
31 |
Correct |
5 ms |
7772 KB |
Output is correct |
32 |
Correct |
6 ms |
8028 KB |
Output is correct |
33 |
Correct |
6 ms |
8280 KB |
Output is correct |
34 |
Correct |
3 ms |
7772 KB |
Output is correct |
35 |
Correct |
3 ms |
7752 KB |
Output is correct |
36 |
Correct |
5 ms |
7772 KB |
Output is correct |
37 |
Correct |
3 ms |
7768 KB |
Output is correct |
38 |
Correct |
4 ms |
8024 KB |
Output is correct |
39 |
Correct |
404 ms |
19812 KB |
Output is correct |
40 |
Correct |
413 ms |
19576 KB |
Output is correct |
41 |
Correct |
373 ms |
19816 KB |
Output is correct |
42 |
Correct |
330 ms |
20168 KB |
Output is correct |
43 |
Correct |
324 ms |
20032 KB |
Output is correct |
44 |
Correct |
318 ms |
20172 KB |
Output is correct |
45 |
Correct |
605 ms |
25052 KB |
Output is correct |
46 |
Correct |
574 ms |
29132 KB |
Output is correct |
47 |
Correct |
109 ms |
20328 KB |
Output is correct |
48 |
Correct |
115 ms |
20868 KB |
Output is correct |
49 |
Correct |
298 ms |
20684 KB |
Output is correct |
50 |
Correct |
99 ms |
20552 KB |
Output is correct |
51 |
Correct |
315 ms |
27852 KB |
Output is correct |