Submission #896639

#TimeUsernameProblemLanguageResultExecution timeMemory
896639borisAngelovBiochips (IZhO12_biochips)C++17
0 / 100
2094 ms415876 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int maxm = 505; const int inf = 1e9; int n, m; int a[maxn]; int root = -1; vector<int> tree[maxn]; int subtreeSize[maxn]; int dp[maxn][maxm]; vector<int> childs[maxn]; void dfs(int node, int par) { subtreeSize[node] = 1; for (int i = 0; i < tree[node].size(); ++i) { if (tree[node][i] != par) { childs[node].push_back(tree[node][i]); dfs(tree[node][i], node); subtreeSize[node] += subtreeSize[tree[node][i]]; } } if (subtreeSize[node] != 1) { for (int i = 0; i < childs[node].size(); ++i) { for (int j = m; j >= 1; --j) { for (int k = m; k >= j; --k) { if (dp[node][k - j] == -inf || dp[childs[node][i]][j] == -inf) { continue; } //cout << node << " " << k << " " << j << " " << childs[node][i] << " " << dp[node][k - j] << " " << dp[childs[node][i]][j] << endl; dp[node][k] = max(dp[node][k], dp[node][k - j] + dp[childs[node][i]][j]); } } } } else { dp[node][1] = a[node]; } /*cout << node << endl; for (int i = 1; i <= m; ++i) { cout << i << " :: " << dp[node][i] << endl; } cout << "----------------------------------------------" << endl;*/ } void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n >> m; for (int i = 1; i <= n; ++i) { int par; cin >> par >> a[i]; if (par == 0) { root = i; } else { tree[par].push_back(i); tree[i].push_back(par); } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { dp[i][j] = -inf; } } if (m == 1) { int ans = 0; for (int i = 1; i <= n; ++i) { ans = max(ans, a[i]); } cout << ans << endl; return 0; } dfs(root, -1); cout << dp[root][m] << endl; return 0; }

Compilation message (stderr)

biochips.cpp: In function 'void dfs(int, int)':
biochips.cpp:25:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |     for (int i = 0; i < tree[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~~~
biochips.cpp:37:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         for (int i = 0; i < childs[node].size(); ++i)
      |                         ~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...