#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> g[n + 1];
for (int i = 1; i < n; i++){
int x, y; cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
vector<bool> used(n + 1);
vector<int> sz(n + 1), d(n + 1);
function<void(int, int)> fill_sz = [&](int v, int pr){
sz[v] = 1;
for (int i: g[v]){
if (i == pr || used[i]) continue;
d[i] = d[v] + 1;
fill_sz(i, v);
sz[v] += sz[i];
}
};
function<int(int, int, int&)> find = [&](int v, int pr, int& S){
for (int i: g[v]){
if (i == pr || used[i]) continue;
if (2 * sz[i] >= S){
return find(i, v, S);
}
}
return v;
};
vector<pii> all;
function<void(int, int, int&)> add = [&](int v, int pr, int& u){
all.pb({v, u});
for (int i: g[v]){
if (i == pr || used[i]) continue;
add(i, v, u);
}
};
vector<int> out(n + 1, 1), p(n + 1);
vector<pii> dd[n + 1];
multiset<int> st;
function<void(int)> arvid = [&](int v){
fill_sz(v, 0);
v = find(v, 0, sz[v]);
used[v] = 1;
d[v] = 0;
fill_sz(v, 0);
all.clear();
for (int i: g[v]){
if (used[i]) continue;
add(i, 0, i);
p[i] = 0;
}
int f = sz[v] / 2;
for (int i = 1; i <= f; i++) dd[i].clear();
for (auto [x, y]: all){
dd[sz[x]].pb({x, y});
}
st.clear();
for (int i = f; i > 0; i--){
for (auto [x, y]: dd[i]){
if (p[y] < d[x]){
if (!p[y]){
st.insert(d[x]);
}
else {
st.erase(st.find(p[y]));
st.insert(d[x]);
}
p[y] = d[x];
}
}
if (st.size() < 2) continue;
auto it = prev(st.end());
int sum = *it + *prev(it) + 1;
out[i] = max(out[i], sum);
}
for (auto [x, y]: all){
int k = min(sz[x], sz[v] - sz[y]);
out[k] = max(out[k], d[x] + 1);
}
for (int i: g[v]){
if (used[i]) continue;
arvid(i);
}
};
arvid(1);
for (int i = n - 1; i > 0; i--) out[i] = max(out[i], out[i + 1]);
for (int i = 1; i <= n; i++){
cout<<((i % 2) ? 1 : out[i / 2])<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
892 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
896 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
4 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
6 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
904 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
456 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
452 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
860 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
892 KB |
Output is correct |
25 |
Correct |
3 ms |
860 KB |
Output is correct |
26 |
Correct |
3 ms |
860 KB |
Output is correct |
27 |
Correct |
3 ms |
896 KB |
Output is correct |
28 |
Correct |
3 ms |
860 KB |
Output is correct |
29 |
Correct |
4 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
4 ms |
1116 KB |
Output is correct |
33 |
Correct |
6 ms |
1116 KB |
Output is correct |
34 |
Correct |
2 ms |
904 KB |
Output is correct |
35 |
Correct |
2 ms |
1116 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
2 ms |
860 KB |
Output is correct |
38 |
Correct |
3 ms |
1116 KB |
Output is correct |
39 |
Correct |
313 ms |
26164 KB |
Output is correct |
40 |
Correct |
314 ms |
25060 KB |
Output is correct |
41 |
Correct |
304 ms |
26040 KB |
Output is correct |
42 |
Correct |
328 ms |
26228 KB |
Output is correct |
43 |
Correct |
301 ms |
26312 KB |
Output is correct |
44 |
Correct |
310 ms |
26564 KB |
Output is correct |
45 |
Correct |
530 ms |
33224 KB |
Output is correct |
46 |
Correct |
481 ms |
38116 KB |
Output is correct |
47 |
Correct |
146 ms |
31940 KB |
Output is correct |
48 |
Correct |
116 ms |
35260 KB |
Output is correct |
49 |
Correct |
294 ms |
27076 KB |
Output is correct |
50 |
Correct |
151 ms |
30652 KB |
Output is correct |
51 |
Correct |
301 ms |
40272 KB |
Output is correct |