//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int N = 2e5 + 66;
const int inf = 1e9;
inline void chk(int &a, int b) {a=max(a,b);}
vector<int>g[N];
int d[N], sz[N];
int tin[N], tout[N], timer, id[N];
void precalc(int v, int p = 1) {
sz[v] = 1;
tin[v] = timer++;
id[tin[v]] = v;
for (int to : g[v]) if (to != p) {
d[to] = d[v] + 1;
precalc(to, v);
sz[v] += sz[to];
}
tout[v] = timer;
}
int D[N], ans[N], cur[N], D2[N];
int n, pq[N];
void dfs(int v, int p = -1, bool cl = 0) {
int mx = -1; for (int to : g[v]) if (to != p) if (mx == -1 || sz[mx] < sz[to]) mx = to;
for (int to : g[v]) if (to != p && to != mx) dfs(to, v, 1);
if (mx != -1) dfs(mx, v, 0);
int V = 2 * d[v];
for (int to : g[v]) if (to != p && to != mx) {
for (int l = tin[to] ; l < tout[to] ; ++ l) {
int a = id[l];
chk(cur[pq[a]], d[a] - V);
chk(D2[pq[a]], d[a]);
}
for (int i = pq[to] ; i >= 1 ; -- i) {
chk(ans[i], cur[i] + D[i]);
chk(D[i], D2[i]);
if (i < sz[to])
chk(D[i], D[i + 1]);
chk(cur[i - 1], cur[i]);
cur[i] = -inf;
}
}
chk(D[pq[v]], d[v]);
for (int i = pq[v] ; i >= 1 ; -- i) {
chk(D[i - 1], D[i]);
}
chk(ans[min(n >> 1, n - sz[v])], D[min(n >> 1, n - sz[v])] + 1 - d[v]);
if (cl) {
for (int i = 0 ; i <= pq[v] ; ++ i) {
cur[i] = D[i] = D2[i] = -inf;
}
}
}
int H = 1;
int t[N * 4];
void modify(int l, int r, int value) {
for (l += H, r += H; l < r; l >>= 1, r >>= 1) {
if (l&1) t[l] = max(t[l], value), l++;
if (r&1) --r, t[r] = max(t[r], value);
}
}
int query(int p) {
int res = -inf;
for (p += H; p > 0; p >>= 1) res = max(res, t[p]);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
while (H < n) H <<= 1;
for (int i = 1 ; i < n ; ++ i) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
for (int i = 0 ; i < N ; ++ i) {
D[i] = D2[i] = cur[i] = -inf;
}
precalc(1);
const int HALF = n / 2;
for (int i = 1 ; i <= n ; ++ i) {
pq[i] = min(HALF, sz[i]);
}
dfs(1);
{
vector<pair<int,int>>vec;
for (int i = 1 ; i <= n ; ++ i) {
vec.emplace_back(n - sz[i], -i);
vec.emplace_back(sz[i], i);
}
for (int i = 0 ; i < H * 2 ; ++ i) t[i] = -inf;
sort(vec.rbegin(), vec.rend());
for (auto [wsz, i] : vec) {
if (i < 0) {
i = -i;
modify(tin[i], tout[i], -d[i] + 1);
} else {
int mx = query(tin[i]);
ans[sz[i]] = max(ans[sz[i]], mx + d[i]);
}
}
}
for (int i = n ; i >= 1 ; -- i) {
ans[i - 1] = max(ans[i - 1], ans[i]);
}
for (int i = 1 ; i <= n ; ++ i) {
if (i % 2 == 1) {
cout << 1 << "\n";
continue;
}
cout << ans[i / 2] + 1 << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
7 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7404 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7532 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
7 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7404 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7532 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
10 ms |
8044 KB |
Output is correct |
23 |
Correct |
9 ms |
7948 KB |
Output is correct |
24 |
Correct |
9 ms |
7916 KB |
Output is correct |
25 |
Correct |
9 ms |
7916 KB |
Output is correct |
26 |
Correct |
9 ms |
7916 KB |
Output is correct |
27 |
Correct |
9 ms |
7916 KB |
Output is correct |
28 |
Correct |
9 ms |
7916 KB |
Output is correct |
29 |
Correct |
9 ms |
7916 KB |
Output is correct |
30 |
Correct |
10 ms |
8044 KB |
Output is correct |
31 |
Correct |
9 ms |
7916 KB |
Output is correct |
32 |
Correct |
12 ms |
8044 KB |
Output is correct |
33 |
Correct |
14 ms |
8172 KB |
Output is correct |
34 |
Correct |
9 ms |
7916 KB |
Output is correct |
35 |
Correct |
9 ms |
7916 KB |
Output is correct |
36 |
Correct |
10 ms |
7916 KB |
Output is correct |
37 |
Correct |
9 ms |
7916 KB |
Output is correct |
38 |
Correct |
11 ms |
8044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
7 ms |
7404 KB |
Output is correct |
6 |
Correct |
6 ms |
7404 KB |
Output is correct |
7 |
Correct |
6 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7404 KB |
Output is correct |
9 |
Correct |
6 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7404 KB |
Output is correct |
11 |
Correct |
6 ms |
7404 KB |
Output is correct |
12 |
Correct |
6 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7404 KB |
Output is correct |
14 |
Correct |
6 ms |
7404 KB |
Output is correct |
15 |
Correct |
6 ms |
7404 KB |
Output is correct |
16 |
Correct |
6 ms |
7404 KB |
Output is correct |
17 |
Correct |
6 ms |
7404 KB |
Output is correct |
18 |
Correct |
6 ms |
7532 KB |
Output is correct |
19 |
Correct |
6 ms |
7404 KB |
Output is correct |
20 |
Correct |
6 ms |
7404 KB |
Output is correct |
21 |
Correct |
6 ms |
7404 KB |
Output is correct |
22 |
Correct |
10 ms |
8044 KB |
Output is correct |
23 |
Correct |
9 ms |
7948 KB |
Output is correct |
24 |
Correct |
9 ms |
7916 KB |
Output is correct |
25 |
Correct |
9 ms |
7916 KB |
Output is correct |
26 |
Correct |
9 ms |
7916 KB |
Output is correct |
27 |
Correct |
9 ms |
7916 KB |
Output is correct |
28 |
Correct |
9 ms |
7916 KB |
Output is correct |
29 |
Correct |
9 ms |
7916 KB |
Output is correct |
30 |
Correct |
10 ms |
8044 KB |
Output is correct |
31 |
Correct |
9 ms |
7916 KB |
Output is correct |
32 |
Correct |
12 ms |
8044 KB |
Output is correct |
33 |
Correct |
14 ms |
8172 KB |
Output is correct |
34 |
Correct |
9 ms |
7916 KB |
Output is correct |
35 |
Correct |
9 ms |
7916 KB |
Output is correct |
36 |
Correct |
10 ms |
7916 KB |
Output is correct |
37 |
Correct |
9 ms |
7916 KB |
Output is correct |
38 |
Correct |
11 ms |
8044 KB |
Output is correct |
39 |
Correct |
324 ms |
26332 KB |
Output is correct |
40 |
Correct |
302 ms |
26096 KB |
Output is correct |
41 |
Correct |
296 ms |
26456 KB |
Output is correct |
42 |
Correct |
372 ms |
26720 KB |
Output is correct |
43 |
Correct |
336 ms |
26828 KB |
Output is correct |
44 |
Correct |
341 ms |
26744 KB |
Output is correct |
45 |
Execution timed out |
4054 ms |
27428 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |