Submission #1255908

#TimeUsernameProblemLanguageResultExecution timeMemory
1255908_dtq_Biochips (IZhO12_biochips)C++20
100 / 100
388 ms405136 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sz(x) (ll)(x.size())
#define all(v) v.begin(), v.end()
#define se second
#define fi first
using namespace std;
const int N = 2e5 + 9;

int n, m, i, j, dp[N][509], root_, a[N], cnt, out[N];

vector<int>vec[N], euler;

void dfs( int x, int root )
{
    cnt ++;

    euler.pb(x);

    for(auto k : vec[x])
        if(k != root) dfs(k, x);

    out[x] = cnt;
}

int main()
{
#define TN "d"
    if(fopen(TN ".in", "r"))
    {
        freopen(TN ".in", "r", stdin);
        freopen(TN ".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;

    for( i = 1; i <= n; i ++ )
    {
        int u, v;

        cin >> u >> v;

        a[i] = v;

        if( u == 0 ) root_ = i;
        else vec[u].pb(i);
    }

    euler.pb(0);

    dfs(root_, 0);

    for( int w = 0; w <= cnt + 1; w ++ )
        for( int j = 1; j <= m; j ++ ) dp[i][j] = -1e18;

    for( int w = sz(euler) - 1; w > 0; w -- )
    {
        int x = euler[w];

        for( int j = 1; j <= m; j ++ )
        {
            dp[w][j] = max(dp[w][j], dp[w + 1][j]);
            dp[w][j] = max(dp[w][j], dp[out[x] + 1][j - 1] + a[x]);
        }
    }

    cout << dp[1][m];

    return 0;
}
/*
*/

Compilation message (stderr)

biochips.cpp: In function 'int main()':
biochips.cpp:56:51: warning: overflow in conversion from 'double' to 'int' changes value from '-1.0e+18' to '-2147483648' [-Woverflow]
   56 |         for( int j = 1; j <= m; j ++ ) dp[i][j] = -1e18;
      |                                                   ^~~~~
biochips.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen(TN ".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
biochips.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen(TN ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...