/**
* author: wxhtzdy
* created: 15.08.2022 17:05:33
**/
#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 = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
--x; --y;
g[x].push_back(y);
g[y].push_back(x);
}
const int L = 20;
vector<int> dep(n);
vector<int> sz(n);
vector<vector<int>> pr(n, vector<int>(L));
function<void(int, int)> Dfs = [&](int v, int p) {
sz[v] = 1;
pr[v][0] = p;
for (int u : g[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
Dfs(u, v);
sz[v] += sz[u];
}
}
};
Dfs(0, 0);
function<int(int, int)> Find = [&](int v, int p) {
for (int u : g[v]) {
if (u != p && sz[u] * 2 >= n) {
return Find(u, v);
}
}
return v;
};
int root = Find(0, 0);
dep[root] = 0;
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
pr[i][j] = pr[pr[i][j - 1]][j - 1];
}
}
Dfs(root, root);
auto LCA = [&](int x, int y) {
if (dep[x] < dep[y]) {
swap(x, y);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[pr[x][i]] >= dep[y]) {
x = pr[x][i];
}
}
if (x == y) {
return x;
}
for (int i = L - 1; i >= 0; i--) {
if (pr[x][i] != pr[y][i]) {
x = pr[x][i];
y = pr[y][i];
}
}
return pr[x][0];
};
auto Dist = [&](int x, int y) {
return dep[x] + dep[y] - 2 * dep[LCA(x, y)];
};
vector<vector<int>> ids(n + 1);
for (int i = 0; i < n; i++) {
ids[sz[i]].push_back(i);
}
vector<int> ans(n);
int x = root, y = root, d = 0;
for (int i = n / 2; i >= 1; i--) {
for (int z : ids[i]) {
int dxz = Dist(x, z);
int dyz = Dist(y, z);
if (dxz > d) {
d = dxz;
y = z;
} else if (dyz > d) {
d = dyz;
x = z;
}
}
ans[i] = d;
}
for (int i = 1; i <= n; i++) {
cout << (i % 2 == 1 ? 1 : ans[i / 2] + 1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |