답안 #37894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
37894 2017-12-28T19:54:12 Z alenam0161 바이오칩 (IZhO12_biochips) C++14
100 / 100
359 ms 407076 KB
#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

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);
             ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 404372 KB Output is correct
2 Correct 3 ms 404372 KB Output is correct
3 Correct 0 ms 404372 KB Output is correct
4 Correct 6 ms 404656 KB Output is correct
5 Correct 3 ms 404696 KB Output is correct
6 Correct 13 ms 404696 KB Output is correct
7 Correct 196 ms 407064 KB Output is correct
8 Correct 213 ms 407076 KB Output is correct
9 Correct 279 ms 407016 KB Output is correct
10 Correct 359 ms 407016 KB Output is correct