#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> sz(n);
function<void(int, int)> DfsSz = [&](int v, int pv) {
sz[v] = 1;
for (int u : g[v]) {
if (u == pv) {
continue;
}
DfsSz(u, v);
sz[v] += sz[u];
}
};
DfsSz(0, 0);
function<int(int, int)> Find = [&](int v, int pv) {
for (int u : g[v]) {
if (u == pv) {
continue;
}
if (sz[u] * 2 >= n) {
return Find(u, v);
}
}
return v;
};
int c = Find(0, 0);
const int L = 25;
vector<vector<int>> jump(n, vector<int>(L));
vector<int> dep(n);
vector<int> dfn(n);
vector<int> dfo(n);
int T = 0;
function<void(int, int)> Dfs = [&](int v, int pv) {
dfn[v] = ++T;
sz[v] = 1;
jump[v][0] = pv;
for (int u : g[v]) {
if (u == pv) {
continue;
}
dep[u] = dep[v] + 1;
Dfs(u, v);
sz[v] += sz[u];
}
dfo[v] = T;
};
Dfs(c, c);
auto LCA = [&](int x, int y) {
if (dep[x] < dep[y]) {
swap(x, y);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[jump[x][i]] >= dep[y]) {
x = jump[x][i];
}
}
if (x == y) {
return x;
}
for (int i = L - 1; i >= 0; i--) {
if (jump[x][i] != jump[y][i]) {
x = jump[x][i];
y = jump[y][i];
}
}
return jump[x][0];
};
auto GetDist = [&](int x, int y) {
return dep[x] + dep[y] - 2 * dep[LCA(x, y)];
};
vector<vector<int>> ver(n + 1);
for (int i = 0; i < n; i++) {
ver[sz[i]].push_back(i);
}
int x = c;
int y = c;
int d = 0;
vector<int> ans(n + 1);
for (int i = n / 2; i >= 1; i--) {
for (int z : ver[i]) {
int d_xz = GetDist(x, z);
int d_yz = GetDist(y, z);
if (d_xz < d_yz) {
swap(x, y);
swap(d_xz, d_yz);
}
if (d_xz > d) {
d = d_xz;
y = z;
}
}
ans[2 * i] = d;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] + 1 << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |