Submission #985602

#TimeUsernameProblemLanguageResultExecution timeMemory
985602OAleksaCat in a tree (BOI17_catinatree)C++14
0 / 100
0 ms348 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 1569; int n, d, dp[N][N]; vector<int> g[N]; void dfs(int v, int p) { vector<int> ch; for (auto u : g[v]) { if (u == p) continue; dfs(u, v); ch.push_back(u); } vector<int> sum(N); for (auto u : ch) { for (int j = 0;j <= d;j++) sum[j] += dp[u][j]; } for (int j = 1;j <= d;j++) { for (auto u : ch) { int x = d - j; if (x < j) { int sm = sum[j - 1] - dp[u][j - 1]; dp[v][j] = max(dp[v][j], sm + dp[u][j - 1]); } else { int sm = sum[x - 1] - dp[u][x - 1]; dp[v][j] = max(dp[v][j], sm + dp[u][j - 1]); } } } dp[v][0] = 1; for (auto u : ch) dp[v][0] += dp[u][d - 1]; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt = 1; //cin >> tt; while (tt--) { cin >> n >> d; for (int i = 2;i <= n;i++) { int x; cin >> x; ++x; g[x].push_back(i); g[i].push_back(x); } dfs(1, 0); int ans = 0; for (int i = 0;i <= d;i++) ans = max(ans, dp[1][i]); cout << ans; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...