This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (rj[i], rj[i + 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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |