Submission #37894

#TimeUsernameProblemLanguageResultExecution timeMemory
37894alenam0161Biochips (IZhO12_biochips)C++14
100 / 100
359 ms407076 KiB
#include <bits/stdc++.h> #define ad push_back using namespace std; const int N = 200007; int cost[N]; vector<int> esim; vector<int> g[N]; int ham[N]; void dfs(int v){ esim.ad(v); for(int to:g[v])dfs(to); ham[v]=esim.size()-1; } int dp[N][507]; int main(){ int root; int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ int P,c; scanf("%d%d",&P,&c); if(P==0){ cost[i]=c; root=i; } else{ cost[i]=c; g[P].ad(i); } } for(int i=1;i<=m;++i)dp[n][i]=-1e9; dfs(root); for(int i=esim.size()-1;i>=0;i--){ int to=esim[i]; for(int j=0;j<=min(m,(int)esim.size()-i);++j){ dp[i][j]=dp[i+1][j]; if(j)dp[i][j]=max(dp[i][j],cost[to]+dp[ham[to]+1][j-1]); } } cout<<dp[0][m]<<endl; return 0; }

Compilation message (stderr)

biochips.cpp: In function 'int main()':
biochips.cpp:19:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d%d",&n,&m);
                       ^
biochips.cpp:22:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&P,&c);
                        ^
biochips.cpp:33:13: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
    dfs(root);
             ^
#Verdict Execution timeMemoryGrader output
Fetching results...