#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) begin(a), end(a)
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
int const N = 1 << 18;
int n;
vector<vector<int>> adj;
int sz[N];
bitset<N> rem;
void dfssz(int ver, int par) {
sz[ver] = 1;
for (auto u : adj[ver]) if (u != par && !rem[u]) {
dfssz(u, ver);
sz[ver] += sz[u];
}
}
int getCentroid(int ver, int par, int msz) {
for (auto u : adj[ver]) if (u != par && !rem[u]) {
if (2 * sz[u] > msz) return getCentroid(u, ver, msz);
}
return ver;
}
int ans[N];
struct SegTree {
int val[2 * N];
void upd(int p, int v, int ver, int lx, int rx, bool smx) {
if (rx - lx == 1) {
if (smx) val[ver] = max(val[ver], v);
else val[ver] = v;
return;
}
int mx = (lx + rx) / 2;
if (p < mx) upd(p, v, ver * 2 + 1, lx, mx, smx);
else upd(p, v, ver * 2 + 2, mx, rx, smx);
val[ver] = max(val[ver * 2 + 1], val[ver * 2 + 2]);
}
int query(int l, int r, int ver, int lx, int rx) {
if (rx <= l || r <= lx) return INT_MIN;
if (l <= lx && rx <= r) return val[ver];
int mx = (lx + rx) / 2;
int a = query(l, r, ver * 2 + 1, lx, mx);
int b = query(l, r, ver * 2 + 2, mx, rx);
return max(a, b);
}
int query(int l, int r) { return query(l, r, 0, 0, N); }
void upd(int p, int v, bool smx) { upd(p, v, 0, 0, N, smx); }
};
SegTree mxDist;
int ub;
void dfs(int ver, int par, int dep, bool write) {
if (write) mxDist.upd(sz[ver], dep, true);
else {
ans[sz[ver]] = max(ans[sz[ver]], dep + mxDist.query(sz[ver], ub) + 1);
}
for (auto u : adj[ver]) if (u != par && !rem[u]) {
dfs(u, ver, dep + 1, write);
}
}
void centroid(int start) {
dfssz(start, -1);
int cen = getCentroid(start, -1, sz[start]);
dfssz(cen, -1);
ub = sz[cen] + 5;
for (int i = 0; i < ub; ++i) {
mxDist.upd(i, 0, false);
}
vector<int> tmp;
for (auto u : adj[cen]) if (!rem[u]) {
tmp.push_back(u);
dfs(u, cen, 1, 0);
dfs(u, cen, 1, 1);
}
for (int i = 0; i < ub; ++i) {
mxDist.upd(i, 0, false);
}
reverse(begin(tmp), end(tmp));
for (auto u : tmp) {
dfs(u, cen, 1, 0);
dfs(u, cen, 1, 1);
}
rem[cen] = true;
for (auto u : adj[cen]) {
if (!rem[u]) centroid(u);
}
}
void solve() {
cin >> n;
adj.resize(n);
for (int i = 0; i < n - 1; ++i) {
int a, b;
cin >> a >> b;
--a; --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
centroid(0);
ans[N-1] = max(ans[N-1], 1);
for (int i = N - 2; i >= 0; --i) ans[i] = max(ans[i], ans[i+1]);
for (int i = 1; i <= n; ++i) {
if (i % 2) cout << 1 << '\n';
else {
cout << ans[i/2] << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
2 ms |
1364 KB |
Output is correct |
11 |
Correct |
2 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
2 ms |
1364 KB |
Output is correct |
11 |
Correct |
2 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
18 ms |
1620 KB |
Output is correct |
23 |
Correct |
19 ms |
1620 KB |
Output is correct |
24 |
Correct |
19 ms |
1644 KB |
Output is correct |
25 |
Correct |
21 ms |
1620 KB |
Output is correct |
26 |
Correct |
18 ms |
1716 KB |
Output is correct |
27 |
Correct |
19 ms |
1620 KB |
Output is correct |
28 |
Correct |
19 ms |
1580 KB |
Output is correct |
29 |
Correct |
19 ms |
1620 KB |
Output is correct |
30 |
Correct |
18 ms |
1628 KB |
Output is correct |
31 |
Correct |
18 ms |
1724 KB |
Output is correct |
32 |
Correct |
27 ms |
1748 KB |
Output is correct |
33 |
Correct |
27 ms |
1788 KB |
Output is correct |
34 |
Correct |
9 ms |
1748 KB |
Output is correct |
35 |
Correct |
8 ms |
1748 KB |
Output is correct |
36 |
Correct |
21 ms |
1620 KB |
Output is correct |
37 |
Correct |
11 ms |
1748 KB |
Output is correct |
38 |
Correct |
17 ms |
1808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1364 KB |
Output is correct |
3 |
Correct |
1 ms |
1364 KB |
Output is correct |
4 |
Correct |
1 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
2 ms |
1364 KB |
Output is correct |
11 |
Correct |
2 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
2 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
1364 KB |
Output is correct |
20 |
Correct |
1 ms |
1364 KB |
Output is correct |
21 |
Correct |
1 ms |
1364 KB |
Output is correct |
22 |
Correct |
18 ms |
1620 KB |
Output is correct |
23 |
Correct |
19 ms |
1620 KB |
Output is correct |
24 |
Correct |
19 ms |
1644 KB |
Output is correct |
25 |
Correct |
21 ms |
1620 KB |
Output is correct |
26 |
Correct |
18 ms |
1716 KB |
Output is correct |
27 |
Correct |
19 ms |
1620 KB |
Output is correct |
28 |
Correct |
19 ms |
1580 KB |
Output is correct |
29 |
Correct |
19 ms |
1620 KB |
Output is correct |
30 |
Correct |
18 ms |
1628 KB |
Output is correct |
31 |
Correct |
18 ms |
1724 KB |
Output is correct |
32 |
Correct |
27 ms |
1748 KB |
Output is correct |
33 |
Correct |
27 ms |
1788 KB |
Output is correct |
34 |
Correct |
9 ms |
1748 KB |
Output is correct |
35 |
Correct |
8 ms |
1748 KB |
Output is correct |
36 |
Correct |
21 ms |
1620 KB |
Output is correct |
37 |
Correct |
11 ms |
1748 KB |
Output is correct |
38 |
Correct |
17 ms |
1808 KB |
Output is correct |
39 |
Correct |
1619 ms |
17744 KB |
Output is correct |
40 |
Correct |
1573 ms |
17200 KB |
Output is correct |
41 |
Correct |
1579 ms |
17532 KB |
Output is correct |
42 |
Correct |
1507 ms |
17868 KB |
Output is correct |
43 |
Correct |
1573 ms |
17916 KB |
Output is correct |
44 |
Correct |
1527 ms |
17876 KB |
Output is correct |
45 |
Correct |
2726 ms |
22760 KB |
Output is correct |
46 |
Correct |
2480 ms |
26828 KB |
Output is correct |
47 |
Correct |
455 ms |
18372 KB |
Output is correct |
48 |
Correct |
357 ms |
18576 KB |
Output is correct |
49 |
Correct |
1708 ms |
18488 KB |
Output is correct |
50 |
Correct |
555 ms |
18772 KB |
Output is correct |
51 |
Correct |
1403 ms |
26032 KB |
Output is correct |