#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);
//F0R(i, n) cout << f[i][2].F << ' '<< f[i][2].S << endl;
FOR(i, 1, n + 1) cout << (i & 1 ? 1 : dp[0][i / 2]) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
420 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
420 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
420 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |