# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
896639 | borisAngelov | Biochips (IZhO12_biochips) | C++17 | 2094 ms | 415876 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |