Submission #89378

# Submission time Handle Problem Language Result Execution time Memory
89378 2018-12-12T18:14:59 Z Vardanyan Biochips (IZhO12_biochips) C++14
0 / 100
2000 ms 400696 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 200*1000+5;
vector<int> g[N];
int X[N];
int n,m;
int dp[N][505];
int knap[N][505];
int ans = 0;
void dfs(int v,int p = -1){
    dp[v][1] = X[v];
 //   if(m == 1) ans = max(ans,dp[v][1]);
    for(int i = 0;i<g[v].size();i++){
        int to = g[v][i];
        if(to == p) continue;
        dfs(to,v);
    }
    int cnt = 0;
    memset(knap,0,sizeof(knap));
    for(int i = 0;i<g[v].size();i++){
        if(g[v][i] == p) continue;
        cnt++;
        for(int x = m;x>=0;x--){
            for(int y = x;y>=0;y--){
                for(int pos = 0;pos<cnt;pos++){
                    knap[cnt][x] = max(knap[cnt][x],knap[pos][x-y]+dp[g[v][i]][y]);
                    dp[v][x] = max(dp[v][x],knap[cnt][x]);
                    if(x == m) ans = max(ans,dp[v][x]);
                }
            }
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    int root;
    for(int i = 1;i<=n;i++){
        int v,x;
        scanf("%d%d",&v,&x);
        X[i] = x;
        if(v == 0) {
            root = i;
            continue;
        }
        g[v].push_back(i);
        g[i].push_back(v);
    }
    dfs(root);
    printf("%d\n",ans);
	return 0;
}

Compilation message

biochips.cpp: In function 'void dfs(int, int)':
biochips.cpp:13:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i<g[v].size();i++){
                   ~^~~~~~~~~~~~
biochips.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i<g[v].size();i++){
                   ~^~~~~~~~~~~~
biochips.cpp: In function 'int main()':
biochips.cpp:35: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:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&v,&x);
         ~~~~~^~~~~~~~~~~~~~
biochips.cpp:48:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dfs(root);
     ~~~^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1158 ms 400376 KB Output is correct
2 Correct 1287 ms 400592 KB Output is correct
3 Execution timed out 2091 ms 400696 KB Time limit exceeded
4 Halted 0 ms 0 KB -