#include <bits/stdc++.h>
#define FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 4000 + 7;
const int ALPHA = 27;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int LOG = 22;
int n;
int dp[N][N], ans[N], sz[N];
PII f[N][N];
VI adj[N];
void init() {
cin >> n;
F0R(i, n - 1) {
int u, v;
cin >> u >> v;
adj[--u].pb(--v);
adj[v].pb(u);
}
return;
}
void ins(PII &g, int x) {
if (x > g.F) {
swap(g.F, g.S);
g.F = x;
}
else if (x > g.S) g.S = x;
return;
}
void preD(int now, int par, int h) {
sz[now] = 1;
F0R(i, n + 1) f[now][i] = {-INF, -INF};
for(int on : adj[now]) if (on != par) {
preD(on, now, h + 1);
sz[now] += sz[on];
FOR(i, 1, n + 1)
ins(f[now][i], f[on][i].F);
}
FOR(i, 1, n + 1) if (sz[now] >= i && f[now][i].F == -INF)
ins(f[now][i], h);
return;
}
void dfs(int now, int par, int h) {
fill(dp[now], dp[now] + n + 1, -INF);
for(int on : adj[now]) if (on != par) {
dfs(on, now, h + 1);
FOR(i, 1, n + 1) {
dp[now][i] = max(dp[now][i], dp[on][i]);
if (n - sz[on] >= i) dp[now][i] = max(dp[now][i], f[on][i].F - h + 1);
}
}
FOR(i, 1, n + 1) dp[now][i] = max(dp[now][i], f[now][i].F + f[now][i].S - 2 * h + 1);
return;
}
int main() {
IOS;
init();
preD(0, -1, 0);
dfs(0, -1, 0);
FOR(i, 1, n + 1) cout << (i & 1 ? 1 : max(dp[0][i / 2], 1)) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
552 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
480 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
552 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
480 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
206 ms |
188224 KB |
Output is correct |
23 |
Correct |
167 ms |
188100 KB |
Output is correct |
24 |
Correct |
171 ms |
186060 KB |
Output is correct |
25 |
Correct |
165 ms |
186828 KB |
Output is correct |
26 |
Correct |
160 ms |
186376 KB |
Output is correct |
27 |
Correct |
162 ms |
188776 KB |
Output is correct |
28 |
Correct |
161 ms |
188680 KB |
Output is correct |
29 |
Correct |
168 ms |
188756 KB |
Output is correct |
30 |
Correct |
161 ms |
188752 KB |
Output is correct |
31 |
Correct |
163 ms |
188808 KB |
Output is correct |
32 |
Correct |
172 ms |
188824 KB |
Output is correct |
33 |
Correct |
172 ms |
189020 KB |
Output is correct |
34 |
Correct |
161 ms |
188680 KB |
Output is correct |
35 |
Correct |
162 ms |
188756 KB |
Output is correct |
36 |
Correct |
175 ms |
188752 KB |
Output is correct |
37 |
Correct |
152 ms |
188788 KB |
Output is correct |
38 |
Correct |
167 ms |
188876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
552 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
480 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
556 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
206 ms |
188224 KB |
Output is correct |
23 |
Correct |
167 ms |
188100 KB |
Output is correct |
24 |
Correct |
171 ms |
186060 KB |
Output is correct |
25 |
Correct |
165 ms |
186828 KB |
Output is correct |
26 |
Correct |
160 ms |
186376 KB |
Output is correct |
27 |
Correct |
162 ms |
188776 KB |
Output is correct |
28 |
Correct |
161 ms |
188680 KB |
Output is correct |
29 |
Correct |
168 ms |
188756 KB |
Output is correct |
30 |
Correct |
161 ms |
188752 KB |
Output is correct |
31 |
Correct |
163 ms |
188808 KB |
Output is correct |
32 |
Correct |
172 ms |
188824 KB |
Output is correct |
33 |
Correct |
172 ms |
189020 KB |
Output is correct |
34 |
Correct |
161 ms |
188680 KB |
Output is correct |
35 |
Correct |
162 ms |
188756 KB |
Output is correct |
36 |
Correct |
175 ms |
188752 KB |
Output is correct |
37 |
Correct |
152 ms |
188788 KB |
Output is correct |
38 |
Correct |
167 ms |
188876 KB |
Output is correct |
39 |
Runtime error |
94 ms |
25112 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |