제출 #985597

#제출 시각아이디문제언어결과실행 시간메모리
985597OAleksaCat in a tree (BOI17_catinatree)C++14
51 / 100
41 ms26060 KiB
#include <bits/stdc++.h> #define f first #define s second using namespace std; const int N = 1569; int n, d, dp[N][N], dp2[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); } if (ch.empty()) dp[v][0] = dp2[v][0] = 1; else { vector<int> sum(N); for (auto u : ch) { for (int j = 0;j <= d;j++) sum[j] += dp2[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] - dp2[u][j - 1]; dp[v][j] = max(dp[v][j], sm + dp[u][j - 1]); } else { int sm = sum[x - 1] - dp2[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]; for (int j = d;j >= 0;j--) dp2[v][j] = max(dp2[v][j + 1], dp[v][j]); } } 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...