#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) (x).begin(),(x).end()
#define pb push_back
const int mxN = 200001;
vector<int> adj[mxN];
int n;
int sz[mxN];
int cpar[mxN];
bool vis[mxN];
void dfs(int n, int p=-1) {
sz[n] = 1;
for (int v : adj[n]) if (v != p && !vis[v]) {
dfs(v, n);
sz[n] += sz[v];
}
}
int centroid(int n) {
dfs(n);
int num = sz[n];
int p = -1;
do {
int nxt = -1;
for (int v : adj[n]) if (v != p && !vis[v]) {
if (2 * sz[v] > num)
nxt = v;
}
p = n, n = nxt;
} while (~n);
return p;
}
int ans[mxN];
int depth[mxN], ssz[mxN];
set<array<int, 3>, greater<array<int, 3>>> s; //sz, depth
void init(int v, int p) {
ssz[v] = 1;
for(int i : adj[v]) {
if(vis[i] || i == p) continue;
depth[i] = depth[v] + 1;
init(i, v);
ssz[v] += ssz[i];
}
}
void add(int v, int p, int rt, int c) {
s.insert({ssz[v], depth[v], rt});
int k = 2 * min(ssz[v], ssz[c] - ssz[rt]);
ans[k] = max(ans[k], depth[v] + 1);
for(int i : adj[v]) {
if(vis[i] || i == p) continue;
add(i, v, rt, c);
}
}
void solve(int c) {
depth[c] = 0;
init(c, -1);
for(int i : adj[c]) {
if(vis[i]) continue;
add(i, c, i, c);
}
multiset<int> ls;
map<int, int> sb;
for(auto[csz, dp, rt] : s) {
if(sb[rt] < dp) {
if(sb[rt] > 0) {
ls.erase(ls.lower_bound(sb[rt]));
}
sb[rt] = dp;
ls.insert(dp);
}
if(ls.size() >= 2) {
int total = 1;
auto it = ls.end();
for(int i = 0; i < 2; ++i) {
--it;
total += *it;
}
ans[csz * 2] = max(ans[csz * 2], total);
}
}
s.clear();
}
void centroid_decomp(int n=0, int p=-1) {
int c = centroid(n);
solve(c);
vis[c] = true;
cpar[c] = p;
for (int v : adj[c]) if (!vis[v]) {
centroid_decomp(v, c);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < n - 1; ++i) {
int a, b; cin >> a >> b; --a; --b;
adj[a].pb(b);
adj[b].pb(a);
}
centroid_decomp();
for(int i = n - 1; 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 << max(1LL, ans[i]) << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5236 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5236 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
11 ms |
5484 KB |
Output is correct |
23 |
Correct |
11 ms |
5484 KB |
Output is correct |
24 |
Correct |
11 ms |
5484 KB |
Output is correct |
25 |
Correct |
11 ms |
5484 KB |
Output is correct |
26 |
Correct |
10 ms |
5484 KB |
Output is correct |
27 |
Correct |
11 ms |
5484 KB |
Output is correct |
28 |
Correct |
12 ms |
5484 KB |
Output is correct |
29 |
Correct |
11 ms |
5484 KB |
Output is correct |
30 |
Correct |
11 ms |
5484 KB |
Output is correct |
31 |
Correct |
11 ms |
5484 KB |
Output is correct |
32 |
Correct |
16 ms |
5740 KB |
Output is correct |
33 |
Correct |
15 ms |
5868 KB |
Output is correct |
34 |
Correct |
9 ms |
5868 KB |
Output is correct |
35 |
Correct |
8 ms |
6124 KB |
Output is correct |
36 |
Correct |
11 ms |
5484 KB |
Output is correct |
37 |
Correct |
9 ms |
5740 KB |
Output is correct |
38 |
Correct |
13 ms |
5868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
5 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5236 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
5 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
5 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
4 ms |
5100 KB |
Output is correct |
22 |
Correct |
11 ms |
5484 KB |
Output is correct |
23 |
Correct |
11 ms |
5484 KB |
Output is correct |
24 |
Correct |
11 ms |
5484 KB |
Output is correct |
25 |
Correct |
11 ms |
5484 KB |
Output is correct |
26 |
Correct |
10 ms |
5484 KB |
Output is correct |
27 |
Correct |
11 ms |
5484 KB |
Output is correct |
28 |
Correct |
12 ms |
5484 KB |
Output is correct |
29 |
Correct |
11 ms |
5484 KB |
Output is correct |
30 |
Correct |
11 ms |
5484 KB |
Output is correct |
31 |
Correct |
11 ms |
5484 KB |
Output is correct |
32 |
Correct |
16 ms |
5740 KB |
Output is correct |
33 |
Correct |
15 ms |
5868 KB |
Output is correct |
34 |
Correct |
9 ms |
5868 KB |
Output is correct |
35 |
Correct |
8 ms |
6124 KB |
Output is correct |
36 |
Correct |
11 ms |
5484 KB |
Output is correct |
37 |
Correct |
9 ms |
5740 KB |
Output is correct |
38 |
Correct |
13 ms |
5868 KB |
Output is correct |
39 |
Correct |
871 ms |
21000 KB |
Output is correct |
40 |
Correct |
861 ms |
20704 KB |
Output is correct |
41 |
Correct |
811 ms |
21036 KB |
Output is correct |
42 |
Correct |
830 ms |
21484 KB |
Output is correct |
43 |
Correct |
806 ms |
21612 KB |
Output is correct |
44 |
Correct |
845 ms |
21356 KB |
Output is correct |
45 |
Correct |
1831 ms |
37612 KB |
Output is correct |
46 |
Correct |
1613 ms |
43628 KB |
Output is correct |
47 |
Correct |
526 ms |
47072 KB |
Output is correct |
48 |
Correct |
514 ms |
55772 KB |
Output is correct |
49 |
Correct |
1025 ms |
27384 KB |
Output is correct |
50 |
Correct |
492 ms |
38624 KB |
Output is correct |
51 |
Correct |
1030 ms |
47072 KB |
Output is correct |