#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 4005, lg = 20;
int n;
vector<int> adj[maxn];
int lift[maxn][lg];
int d[maxn], sz[maxn];
void dfs(int u) {
sz[u] = 1;
for (int i=1;i<lg;i++) {
if (lift[u][i-1]==-1 || lift[lift[u][i-1]][i-1]==-1) break;
lift[u][i] = lift[lift[u][i-1]][i-1];
}
for (int v:adj[u]) {
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
lift[v][0] = u;
d[v] = d[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
int lca(int u, int v) {
if (d[u] > d[v]) swap(u, v);
for (int i=lg-1;i>=0;i--) {
if (d[v] - (1<<i) >= d[u]) v = lift[v][i];
}
if (u==v) return u;
for (int i=lg-1;i>=0;i--) if (lift[u][i]!=lift[v][i]) {
u = lift[u][i], v = lift[v][i];
}
return lift[u][0];
}
int lift2(int u, int D) {
for (int i=lg-1;i>=0;i--) {
if (d[u] - (1<<i) >= D) u = lift[u][i];
}
return u;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i=1;i<=n;i++) for (int j=0;j<lg;j++) lift[i][j] = -1;
d[1] = 0;
dfs(1);
int ans[n/2+1];
for (int i=1;i<=n/2;i++) ans[i] = 1;
for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) {
int m = lca(i, j);
int a = sz[i], b = sz[j];
if (m==i) {
int thing = lift2(j, d[i]+1);
a = n - sz[thing];
}
if (m==j) {
int thing = lift2(i, d[j]+1);
b = n - sz[thing];
}
int dist = d[i] + d[j] - d[m] * 2;
// cout << i << " " << j << " " << dist << endl;
// cout << a << " " << b << endl;
ans[min(a, b)] = max(dist + 1, ans[min(a, b)]);
}
for (int i=n/2-1;i>=1;i--) ans[i] = max(ans[i], ans[i+1]);
for (int i=1;i<=n;i++) {
if (i%2==0) cout << ans[i/2] << endl;
else cout << "1\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
564 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
564 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
692 ms |
1416 KB |
Output is correct |
23 |
Correct |
674 ms |
1368 KB |
Output is correct |
24 |
Correct |
632 ms |
1412 KB |
Output is correct |
25 |
Correct |
635 ms |
1372 KB |
Output is correct |
26 |
Correct |
638 ms |
1412 KB |
Output is correct |
27 |
Correct |
655 ms |
1368 KB |
Output is correct |
28 |
Correct |
658 ms |
1420 KB |
Output is correct |
29 |
Correct |
661 ms |
1416 KB |
Output is correct |
30 |
Correct |
656 ms |
1372 KB |
Output is correct |
31 |
Correct |
671 ms |
1428 KB |
Output is correct |
32 |
Correct |
1016 ms |
1372 KB |
Output is correct |
33 |
Correct |
1188 ms |
1584 KB |
Output is correct |
34 |
Correct |
616 ms |
1372 KB |
Output is correct |
35 |
Correct |
623 ms |
1628 KB |
Output is correct |
36 |
Correct |
776 ms |
1424 KB |
Output is correct |
37 |
Correct |
596 ms |
1368 KB |
Output is correct |
38 |
Correct |
799 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
564 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
692 ms |
1416 KB |
Output is correct |
23 |
Correct |
674 ms |
1368 KB |
Output is correct |
24 |
Correct |
632 ms |
1412 KB |
Output is correct |
25 |
Correct |
635 ms |
1372 KB |
Output is correct |
26 |
Correct |
638 ms |
1412 KB |
Output is correct |
27 |
Correct |
655 ms |
1368 KB |
Output is correct |
28 |
Correct |
658 ms |
1420 KB |
Output is correct |
29 |
Correct |
661 ms |
1416 KB |
Output is correct |
30 |
Correct |
656 ms |
1372 KB |
Output is correct |
31 |
Correct |
671 ms |
1428 KB |
Output is correct |
32 |
Correct |
1016 ms |
1372 KB |
Output is correct |
33 |
Correct |
1188 ms |
1584 KB |
Output is correct |
34 |
Correct |
616 ms |
1372 KB |
Output is correct |
35 |
Correct |
623 ms |
1628 KB |
Output is correct |
36 |
Correct |
776 ms |
1424 KB |
Output is correct |
37 |
Correct |
596 ms |
1368 KB |
Output is correct |
38 |
Correct |
799 ms |
1368 KB |
Output is correct |
39 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |