1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #include <bits/stdc++.h> #define nmf(i, s, e) for (int i = s; i <= e; i++) #define ref(i, s, e) for (int i = s; i >= e; i--) namespace FastIO { template <typename T> inline void read(T &x) { short neg = 1; char ch; while (ch = getchar(), !isdigit(ch)) if (ch == '-') neg = -1; x = ch - '0'; while (ch = getchar(), isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ '0'); x *= neg; } template <typename T, typename... Args> inline void read(T &x, Args &...args) { read(x); read(args...); } template <typename T> inline void read(T *begin, T *end) { unsigned len = end - begin; for (unsigned i = 0; i < len; i++) read(*(begin + i)); } template <typename T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T, typename... Args> inline void write(T x, Args... args) { write(x); putchar(' '); write(args...); } template <typename T> inline void write(T *begin, T *end) { unsigned len = end - begin; for (unsigned i = 0; i < len; i++) write(*(begin + i)), putchar(' '); } } using namespace FastIO; using namespace std; typedef long long LL; typedef unsigned long long uLL; int n; vector<int> grh[200005]; int ans[200005]; int euler[400005], pos[200005], dep[200005], sz[200005], tim; int st[400005][19], lg[400005]; int weight[200005]; int centroid; void dfs1(int u, int fa) { euler[++tim] = u; pos[u] = tim; sz[u] = 1; for (auto v : grh[u]) { if (v == fa) continue; dep[v] = dep[u] + 1; dfs1(v, u); euler[++tim] = u; sz[u] += sz[v]; weight[u] = max(weight[u], sz[v]); } weight[u] = max(weight[u], n - sz[u]); if (weight[u] <= n >> 1) { centroid = u; } } void LCA_init() { lg[0] = -1; nmf(i, 1, tim) lg[i] = lg[i >> 1] + 1; nmf(i, 1, tim) st[i][0] = euler[i]; nmf(j, 1, 18) nmf(i, 1, tim - (1 << j) + 1) st[i][j] = (dep[st[i][j - 1]] < dep[st[i + (1 << (j - 1))][j - 1]]) ? st[i][j - 1] : st[i + (1 << (j - 1))][j - 1]; } int LCA(int x, int y) { int l = pos[x], r = pos[y]; if (l > r) swap(l, r); int k = lg[(r - l + 1)]; return (dep[st[l][k]] < dep[st[r - (1 << k) + 1][k]]) ? st[l][k] : st[r - (1 << k) + 1][k]; } int dist(int x, int y) { if (x == y) return 0; if (x == 1) return dep[y]; if (y == 1) return dep[x]; return dep[x] + dep[y] - (dep[LCA(x, y)] << 1); } vector<pair<int, int>> q; vector<int> buc[200005]; void dfs2(int u, int fa) { sz[u] = 1; for (auto v : grh[u]) { if (v == fa) continue; dfs2(v, u); sz[u] += sz[v]; } if (u ^ centroid) buc[sz[u]].emplace_back(u); } void solve() { read(n); nmf(i, 2, n) { int ui, vi; read(ui, vi); grh[ui].emplace_back(vi); grh[vi].emplace_back(ui); } dfs1(1, 0); LCA_init(); memset(sz, 0, sizeof(sz)); dfs2(centroid, 0); ref(i, n, 1) { for (auto j : buc[i]) q.emplace_back(i, j); } auto it = q.begin(); int x = centroid, y = centroid; ref(i, n, 1) { if (i & 1) { ans[i] = 1; continue; } while (it != q.end() && it->first >= (i >> 1)) { int New = it->second; if (dist(x, New) > dist(x, y)) y = New; if (dist(New, y) > dist(x, y)) x = New; it++; } ans[i] = dist(x, y) + 1; } nmf(i, 1, n) write(ans[i]), putchar('\n'); return; } signed main() { int T = 1; while (T--) solve(); return 0; }
|