Submission #237353

#TimeUsernameProblemLanguageResultExecution timeMemory
237353IgorICat in a tree (BOI17_catinatree)C++17
51 / 100
18 ms16256 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2000; const int K = 1; int n, d; int dp[N][2000]; int p[N]; vector<int> graph[N]; void dfs1(int v) { for (auto u : graph[v]) dfs1(u); dp[v][0] = 1; for (auto u : graph[v]) dp[v][0] += dp[u][d - 1]; for (int j = d; j > 0; j--) { if (2 * j >= d) { for (auto u : graph[v]) { dp[v][j] += dp[u][j - 1]; } } else { dp[v][j] = dp[v][d - j]; int r = 0; for (auto u : graph[v]) { r = max(r, dp[u][j - 1] - dp[u][d - j - 1]); } dp[v][j] += r; } } for (int j = d - 1; j >= 0; j--) dp[v][j] = max(dp[v][j], dp[v][j + 1]); } int max_place[N]; void dfs2(int v) { for (auto u : graph[v]) dfs2(u); // dp[v][k] - nearest to v if placed k dp[v][0] = 1e9; vector<int> Up; for (auto u : graph[v]) { for (int i = 1; dp[u][i] != -1; i++) { Up.push_back(min(d, dp[u][i] + 1)); } } sort(Up.begin(), Up.end()); reverse(Up.begin(), Up.end()); int lka = 1e9, rka = 0; for (int i = 0; i < Up.size(); i++) { if (Up[i] * 2 >= d) { lka = min(lka, i + 1); //cout << "set " << i + 1 << " " << Up[i] << endl; dp[v][i + 1] = Up[i]; rka = max(rka, i + 1); } } for (int i = rka - 1; i >= 0; i--) dp[v][i] = max(dp[v][i], dp[v][i + 1]); /*for (auto u : graph[v]) { for (int i = 0; dp[u][i] != -1; i++) { int d1 = dp[u][i] + 1; int d2 = max(d1, d - d1); int L = lka - 1, R = rka; while (L + 1 < R) { int M = (L + R) / 2; if (dp[v][M] >= d2) L = M; else R = M; } if (L == lka - 1) L = 0; int will_have = L + i; for (int j = 0; dp[u][j] != 1; j++) { if (dp[u][j] >= d2) { will_have -= j; cout << "TAPOK ! " << j << endl; break; } } dp[v][will_have] = max(dp[v][will_have], d1); } }*/ for (auto u : graph[v]) { for (int i = 0; dp[u][i] != -1; i++) { int d1 = dp[u][i] + 1; int d2 = max(d1, d - d1) - 1; int will_have = i; for (auto w : graph[v]) if (w != u) { for (int j = 1; dp[w][j] != -1; j++) { if (dp[w][j] >= d2) { will_have++; } } } //cout << u << " " << i << " " << d1 << " " << d2 << " " << will_have << endl; //cout << u << "->" << i << " " << will_have << endl; dp[v][will_have] = max(dp[v][will_have], d1); } } for (int i = 0; dp[v][i] != -1; i++) { max_place[v] = i; if (dp[v][i] >= d && dp[v][i + 1] == -1) { dp[v][i + 1] = 0; } } /*cout << v << ": "; for (int i = 0; dp[v][i] != -1; i++) { cout << dp[v][i] << " "; } cout << endl;*/ } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> d; if (d == 1) cout << n, exit(0); for (int i = 1; i < n; i++) { cin >> p[i]; graph[p[i]].push_back(i); } if (d < K) { dfs1(0); cout << dp[0][0] << endl; } else { for (int i = 0; i < N; i++) for (int j = 0; j < 2000; j++) dp[i][j] = -1; dfs2(0); cout << max_place[0] << endl; } }

Compilation message (stderr)

catinatree.cpp: In function 'void dfs2(int)':
catinatree.cpp:61:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < Up.size(); i++)
                     ~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...