Submission #85245

# Submission time Handle Problem Language Result Execution time Memory
85245 2018-11-19T01:31:20 Z zoooma13 Biochips (IZhO12_biochips) C++14
0 / 100
2000 ms 5096 KB
/**
time complexity : O(N*M^2)
*/

#include <bits/stdc++.h>
using namespace std;

#define MAX_N 10004
#define MAX_M 102
#define INF 0x3f3f3f3f

int N ,M;
int A ,B;
int Mem[MAX_N];
vector <int> Adj[MAX_N];

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

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

        for(int j=1; j<=m; j++)
        {
            curr = Solve(i ,j);
            for(int k=0; k<=m-j; k++)
                dp_new[j+k] = max(dp_new[j+k] ,dp_all[k] + curr);
        }

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

    for(int l=0; l<=m; l++)
        dp[l][v] = dp_all[l];
    dp[1][v] = max(dp[1][v] ,Mem[v]);

    return dp[m][v];
}

int main()
{
    scanf("%d%d",&N,&M); assert(N < MAX_N && M < MAX_M);

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

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

    memset(dp ,-1 ,sizeof dp);
    printf("%d\n",Solve(root ,M));
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&N,&M); assert(N < MAX_N && M < MAX_M);
     ~~~~~^~~~~~~~~~~~~~
biochips.cpp:55: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:65:11: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
     printf("%d\n",Solve(root ,M));
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4472 KB Output is correct
2 Correct 5 ms 4600 KB Output is correct
3 Correct 5 ms 4672 KB Output is correct
4 Correct 675 ms 5008 KB Output is correct
5 Correct 1277 ms 5080 KB Output is correct
6 Execution timed out 2050 ms 5096 KB Time limit exceeded
7 Halted 0 ms 0 KB -