# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1109579 | 12345678 | Biochips (IZhO12_biochips) | C++17 | 191 ms | 19552 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 nx=2e5+5, kx=505;
int n, m, p, sz[nx], rt;
vector<int> d[nx];
vector<int> dp[nx];
void dfs(int u)
{
for (auto v:d[u])
{
dfs(v);
if (dp[v].size()>dp[u].size()) swap(dp[u], dp[v]);
//if (u==3) cout<<"debug "<<u<<' '<<dp[u].size()<<' '<<v<<' '<<dp[v].size()<<'\n';
if (dp[v].empty()) continue;
int tmp=min(m+1, (int)dp[u].size()+(int)dp[v].size()-1);
while (dp[u].size()<tmp) dp[u].push_back(0);
//if (u==3) cout<<"debug "<<u<<' '<<dp[u].size()<<' '<<v<<' '<<dp[v].size()<<'\n';
//if (u==3) for (int i=0; i<dp[u].size(); i++) cout<<dp[u][i]<<' ';
//if (u==3) cout<<'\n';
vector<int> nxt(tmp);
for (int i=1; i<dp[v].size(); i++)
{
for (int j=i; j<tmp; j++) nxt[j]=max(nxt[j], dp[u][j-i]+dp[v][i]);
}
for (int i=1; i<tmp; i++) dp[u][i]=max(dp[u][i], nxt[i]);
dp[v].clear();
}
if (dp[u].size()<2) dp[u].push_back(0), dp[u].push_back(0);
dp[u][1]=max(dp[u][1], sz[u]);
//cout<<"finish "<<u<<":";
//for (int i=0; i<dp[u].size(); i++) cout<<dp[u][i]<<' ';
//cout<<'\n';
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++)
{
cin>>p>>sz[i];
if (p) d[p].push_back(i);
else rt=i;
}
dfs(rt);
cout<<dp[rt][m];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |