# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
867367 | Requiem | Biochips (IZhO12_biochips) | C++17 | 407 ms | 405808 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |