Submission #40288

# Submission time Handle Problem Language Result Execution time Memory
40288 2018-01-30T17:11:14 Z TAMREF Biochips (IZhO12_biochips) C++11
100 / 100
417 ms 405588 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mx = 200005;

int dt[mx], ft[mx], clk;
int ord[mx], num;
int n, m;
int mem[mx];
int dp[mx][505];

vector<int> Child[mx];

void dfs(int x){
    if(Child[x].empty()) ++clk;
    dt[x] = clk;
    for(int &u : Child[x]){
        dfs(u);
    }
    ft[x] = clk;
    ord[num++] = x;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin >> n >> m;

    int rt;

    for(int i=1,p;i<=n;i++){
        cin >> p >> mem[i];
        if(p) Child[p].push_back(i);
        else rt = i;
    }

    dfs(rt);

    for(int i=0;i<n;i++){
        int u = ord[i];
        int l = dt[u], r = ft[u];

        for(int j = 1; j <= m; j++) dp[r][j] = max( dp[r-1][j], dp[r][j] );
        for(int j = 1; j <= m; j++) dp[r][j] = max( dp[l-1][j-1] + mem[u], dp[r][j]);
    }

    int ans = 0;

    for(int i=1;i<=clk;i++) ans = max(dp[i][m], ans);

    cout << ans << endl;
    /*
    for(int i=1;i<=clk;i++){
        printf("Clock = %d : ",i);
        for(int j=0;j<=m;j++){
            printf("%d ",dp[i][j]);
        }
        puts("");
    }
    */
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:37:12: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dfs(rt);
            ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 404532 KB Output is correct
2 Correct 0 ms 404532 KB Output is correct
3 Correct 0 ms 404532 KB Output is correct
4 Correct 3 ms 404664 KB Output is correct
5 Correct 3 ms 404664 KB Output is correct
6 Correct 6 ms 404664 KB Output is correct
7 Correct 256 ms 405588 KB Output is correct
8 Correct 244 ms 405588 KB Output is correct
9 Correct 270 ms 405588 KB Output is correct
10 Correct 417 ms 405588 KB Output is correct