Submission #339462

# Submission time Handle Problem Language Result Execution time Memory
339462 2020-12-25T10:43:02 Z Vladikus004 Biochips (IZhO12_biochips) C++14
100 / 100
553 ms 402280 KB
#include <bits/stdc++.h>
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define inf 2e9
#define ff first
#define ss second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 200000 + 1, M = 500 + 1;
int n, m, sz[N], w[N], dp[N][M];
vector <int> v[N], ord;

void dfs(int x){
    ord.pb(x);
    for (auto u: v[x]){
        dfs(u);
        sz[x] += sz[u];
    }
    sz[x]++;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    freopen("input.txt", "r", stdin);
    cin >> n >> m;
    int st;
    for (int i = 0; i < n; i++){
        int x;
        cin >> x >> w[i];
        if (x)
            v[x-1].pb(i);
        else st = i;
    }
    dfs(st);
    memset(dp, -1, sizeof dp);
    for (int i = 0; i <= ord.size(); i++)
        dp[i][0] = 0;
    int ans = 0;
    for (int i = ord.size()-1; i >= 0; i--){
        for (int j = 1; j <= m; j++){
            if (dp[i+sz[ord[i]]][j-1] != -1)
                dp[i][j] = dp[i+sz[ord[i]]][j-1] + w[ord[i]];
            dp[i][j] = max(dp[i][j], dp[i + 1][j]);
        }
        ans = max(ans, dp[i][m]);
    }
    cout << ans;
    return 0;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:41:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |     for (int i = 0; i <= ord.size(); i++)
      |                     ~~^~~~~~~~~~~~~
biochips.cpp:39:8: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
   39 |     dfs(st);
      |     ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 200 ms 397276 KB Output is correct
2 Correct 201 ms 397164 KB Output is correct
3 Correct 201 ms 397292 KB Output is correct
4 Correct 209 ms 397676 KB Output is correct
5 Correct 207 ms 397548 KB Output is correct
6 Correct 207 ms 397804 KB Output is correct
7 Correct 390 ms 401376 KB Output is correct
8 Correct 389 ms 401632 KB Output is correct
9 Correct 474 ms 402024 KB Output is correct
10 Correct 553 ms 402280 KB Output is correct