Submission #867367

# Submission time Handle Problem Language Result Execution time Memory
867367 2023-10-28T09:34:42 Z Requiem Biochips (IZhO12_biochips) C++17
90 / 100
407 ms 405808 KB
#include<bits/stdc++.h>
// #define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e9
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "biochips"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 2e5 + 9;
int root,n,k;
vector<int> g[MAXN];

void solve(){
    cin>>n>>k;
    vector<int> par(n+3),etour(n+3), in(n+3), out(n+3), value(n+3);
    vector<vector<int>> dp(n+3,vector<int>(k+3,0));
    int id = 0;
    function<void(int,int)> dfs = [&](int u,int p){
        etour[++id] = u;
        in[u] = id;
        for(auto v: g[u]){
            if (v==p) continue;
            dfs(v,u);
        }
        out[u] = id;
    };
    for(int i=1;i<=n;i++){
        cin>>par[i]>>value[i];
        if (par[i] == 0) root = i;
        else {
            g[i].pb(par[i]);
            g[par[i]].pb(i);
        }
    }
    dfs(root,-1);
    for(int i=1;i<=k;i++){
        dp[n+1][i] = -INF;
    }
    dp[n + 1][0] = 0;
    for (int i = n; i >= 1; i--){
        for(int j=0;j<=k;j++){
            int u = etour[i];
            maximize(dp[i][j],dp[i+1][j]);
            if (j>=1) maximize(dp[i][j],dp[out[u]+1][j-1] + value[u]);
        }
    }
    cout<<dp[1][k]<<endl;

}
main()
{
    fast;
    // freopen(TASKNAME".inp","r",stdin);
    // freopen(TASKNAME".out","w",stdout);
    solve();
}

Compilation message

biochips.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   62 | main()
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Incorrect 1 ms 5208 KB Output isn't correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 5 ms 8028 KB Output is correct
5 Correct 8 ms 9052 KB Output is correct
6 Correct 12 ms 10072 KB Output is correct
7 Correct 223 ms 194904 KB Output is correct
8 Correct 216 ms 194720 KB Output is correct
9 Correct 307 ms 300168 KB Output is correct
10 Correct 407 ms 405808 KB Output is correct