#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 200005;
int n, u, v, ans[MN], dep[MN], sz[MN], vis[MN], bit[MN], rsz; //deepest dep with this size
vector<int> a[MN], upds;
void upd(int x, int val) {
for (int i = x; i < MN; i += i & -i) bit[i] = max(bit[i], val);
upds.push_back(x);
}
int qry(int x) {
int ret = 0;
for (int i = x; i > 0; i -= i & -i) ret = max(ret, bit[i]);
return ret;
}
void rev(int x) {
for (int i = x; i < MN; i += i & -i) bit[i] = 0;
}
void reset() {
//memset(bit, 0, sizeof(bit));
while (upds.size()) rev(upds.back()), upds.pop_back();
}
int get_size(int cur, int pre) {
sz[cur] = 1;
for (int nxt : a[cur]) {
if (vis[nxt] || nxt == pre) continue;
sz[cur] += get_size(nxt, cur);
}
return sz[cur];
}
int get_cent(int cur, int pre, int siz) {
for (int nxt : a[cur]) {
if (vis[nxt] || nxt == pre) continue;
if (sz[nxt] * 2 > siz) return get_cent(nxt, cur, siz);
}
return cur;
}
void dfs(int cur, int pre, int d) {
dep[cur] = d;
int md = qry(n - sz[cur] + 1);
if (md) {
//if (sz[cur] == 3) printf("%d %d %d\n", cur, sz[cur], d + md + 1);
ans[sz[cur] * 2] = max(ans[sz[cur] * 2], d + md + 1);
}
ans[min(rsz, sz[cur]) * 2] = max(ans[min(rsz, sz[cur]) * 2], d + 1);
for (int nxt : a[cur]) {
if (vis[nxt] || nxt == pre) continue;
dfs(nxt, cur, d + 1);
}
}
void dfs(int cur, int pre) {
//printf("upd: %d %d %d\n", cur, sz[cur], dep[cur]);
upd(n - sz[cur] + 1, dep[cur]);
for (int nxt : a[cur]) {
if (vis[nxt] || nxt == pre) continue;
dfs(nxt, cur);
}
}
void dfs1(int cur, int pre) {
sz[cur] = 1;
for (int nxt : a[cur]) {
if (nxt == pre) continue;
dfs1(nxt, cur);
sz[cur] += sz[nxt];
}
}
void solve(int cur) {
cur = get_cent(cur, cur, get_size(cur, cur));
get_size(cur, cur);
//printf("cur: %lld\n", cur);
reset();
for (int nxt : a[cur]) {
if (vis[nxt]) continue;
rsz = n - sz[nxt];
dfs(nxt, cur, 1);
dfs(nxt, cur);
}
reverse(a[cur].begin(), a[cur].end());
reset();
for (int nxt : a[cur]) {
if (vis[nxt]) continue;
rsz = n - sz[nxt];
dfs(nxt, cur, 1);
dfs(nxt, cur);
}
vis[cur] = 1;
for (int nxt : a[cur]) {
if (vis[nxt]) continue;
solve(nxt);
}
}
int32_t main() {
boost();
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
a[u].push_back(v);
a[v].push_back(u);
}
solve(1);
dfs1(1, 0);
for (int i = 2; i <= n; i++) {
int csz = min(sz[i], n - sz[i]);
ans[csz * 2] = max(ans[csz * 2], 2ll);
}
for (int i = n; i > 0; i--) ans[i] = max({1ll, ans[i + 1], ans[i]});
for (int i = 1; i <= n; i++) printf("%lld\n", i % 2 ? 1 : ans[i]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
11 ms |
5420 KB |
Output is correct |
23 |
Correct |
10 ms |
5416 KB |
Output is correct |
24 |
Correct |
10 ms |
5324 KB |
Output is correct |
25 |
Correct |
11 ms |
5324 KB |
Output is correct |
26 |
Correct |
11 ms |
5324 KB |
Output is correct |
27 |
Correct |
10 ms |
5324 KB |
Output is correct |
28 |
Correct |
10 ms |
5324 KB |
Output is correct |
29 |
Correct |
12 ms |
5324 KB |
Output is correct |
30 |
Correct |
10 ms |
5412 KB |
Output is correct |
31 |
Correct |
10 ms |
5324 KB |
Output is correct |
32 |
Correct |
14 ms |
5456 KB |
Output is correct |
33 |
Correct |
13 ms |
5452 KB |
Output is correct |
34 |
Correct |
8 ms |
5324 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
10 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5324 KB |
Output is correct |
38 |
Correct |
9 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
11 ms |
5420 KB |
Output is correct |
23 |
Correct |
10 ms |
5416 KB |
Output is correct |
24 |
Correct |
10 ms |
5324 KB |
Output is correct |
25 |
Correct |
11 ms |
5324 KB |
Output is correct |
26 |
Correct |
11 ms |
5324 KB |
Output is correct |
27 |
Correct |
10 ms |
5324 KB |
Output is correct |
28 |
Correct |
10 ms |
5324 KB |
Output is correct |
29 |
Correct |
12 ms |
5324 KB |
Output is correct |
30 |
Correct |
10 ms |
5412 KB |
Output is correct |
31 |
Correct |
10 ms |
5324 KB |
Output is correct |
32 |
Correct |
14 ms |
5456 KB |
Output is correct |
33 |
Correct |
13 ms |
5452 KB |
Output is correct |
34 |
Correct |
8 ms |
5324 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
10 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5324 KB |
Output is correct |
38 |
Correct |
9 ms |
5452 KB |
Output is correct |
39 |
Correct |
1278 ms |
23488 KB |
Output is correct |
40 |
Correct |
1162 ms |
22832 KB |
Output is correct |
41 |
Correct |
1078 ms |
23216 KB |
Output is correct |
42 |
Correct |
1022 ms |
23584 KB |
Output is correct |
43 |
Correct |
1027 ms |
23580 KB |
Output is correct |
44 |
Correct |
1022 ms |
23628 KB |
Output is correct |
45 |
Correct |
2091 ms |
27984 KB |
Output is correct |
46 |
Correct |
1580 ms |
30312 KB |
Output is correct |
47 |
Correct |
288 ms |
23344 KB |
Output is correct |
48 |
Correct |
199 ms |
23588 KB |
Output is correct |
49 |
Correct |
967 ms |
24268 KB |
Output is correct |
50 |
Correct |
332 ms |
23720 KB |
Output is correct |
51 |
Correct |
967 ms |
29524 KB |
Output is correct |