답안 #85197

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
85197 2018-11-18T22:13:12 Z zoooma13 바이오칩 (IZhO12_biochips) C++14
0 / 100
282 ms 6576 KB
#include <bits/stdc++.h>
using namespace std;

#define MAX_N 200005
#define MAX_M 502
#define INF 0x3f3f3f3f

int N ,M;
int A ,B;

vector <int> Adj[MAX_N];
int Mem[MAX_N];

int dp[MAX_M][MAX_M];
int Solve(int Q ,int M)
{
    if(Adj[Q].empty())
        return (M == 1 ? Mem[Q] : -INF);
    if(~dp[M][Q])
        return dp[M][Q];

    int dp_new[M+1] ,dp_all[M+1] = {0};
    for(int i : Adj[Q])
    {
        memset(dp_new ,-INF ,sizeof dp_new);

        for(int j=1; j<=M; j++)
        for(int h=0; h<=M-j; h++)
            dp_new[j+h] = max(dp_new[j+h] ,dp_all[h]+Solve(i ,j));

        for(int h=0; h<=M; h++)
            dp_all[h] = max(dp_all[h] ,dp_new[h]);
    }

    return dp[M][Q] = (M == 1 ? max(dp_all[M] ,Mem[Q]) : dp_all[M]);
}

int main()
{
    scanf("%d%d",&N,&M);

    int root;
    for(int i=0; i<N; i++)
    {
        scanf("%d%d",&A,&B);

        Mem[i+1] = B;
        if(A == 0)
            root = i+1;
        else
            Adj[A].push_back(i+1);
    }

    memset(dp ,-1 ,sizeof dp);
    cout << Solve(root ,M) << endl;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&N,&M);
     ~~~~~^~~~~~~~~~~~~~
biochips.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~
biochips.cpp:55:26: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
     cout << Solve(root ,M) << endl;
                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6008 KB Output is correct
2 Correct 6 ms 6136 KB Output is correct
3 Correct 7 ms 6172 KB Output is correct
4 Incorrect 282 ms 6576 KB Output isn't correct
5 Halted 0 ms 0 KB -