# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
516798 | Be_dos | 바이오칩 (IZhO12_biochips) | C++17 | 672 ms | 390780 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 <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <unordered_set>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <random>
#include <cfloat>
#include <chrono>
#include <bitset>
#include <complex>
#include <immintrin.h>
#include <cassert>
void dfs2(int32_t v, std::vector<int32_t>* tree, int32_t* subtree_sizes) {
subtree_sizes[v] = 1;
for(int32_t i = 0; i < tree[v].size(); i++) {
dfs2(tree[v][i], tree, subtree_sizes);
subtree_sizes[v] += subtree_sizes[tree[v][i]];
}
}
int32_t m;
void dfs(int32_t v, std::vector<int32_t>* tree, int32_t** dp, int32_t* arr, int32_t* subtree_sizes) {
if(tree[v].size() == 0) {
dp[v] = new int32_t[m + 1];
dp[v][0] = 0;
dp[v][1] = arr[v];
for(int32_t i = 2; i <= m; i++)
dp[v][i] = -1'000'000'000;
return;
}
int32_t best_ind = 0;
for(int32_t i = 0; i < tree[v].size(); i++)
if(subtree_sizes[tree[v][i]] > subtree_sizes[tree[v][best_ind]])
best_ind = i;
std::swap(tree[v][best_ind], tree[v][0]);
for(int32_t i = 0; i < tree[v].size(); i++)
dfs(tree[v][i], tree, dp, arr, subtree_sizes);
dp[v] = dp[tree[v][0]];
int32_t* tmp = new int32_t[m + 1];
for(int32_t i = 1; i < tree[v].size(); i++) {
for(int32_t j = 0; j <= m; j++) {
tmp[j] = -1'000'000'000;
for(int32_t k = 0; k <= std::min(j, subtree_sizes[tree[v][i]]); k++)
tmp[j] = std::max(tmp[j], dp[v][j - k] + dp[tree[v][i]][k]);
}
int32_t* tmp2 = dp[v];
dp[v] = tmp;
tmp = tmp2;
}
dp[v][1] = std::max(dp[v][1], arr[v]);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int32_t n;
std::cin >> n >> m;
int32_t* arr = new int32_t[n];
std::vector<int32_t>* tree = new std::vector<int32_t>[n];
int32_t root = -1;
for(int32_t i = 0; i < n; i++) {
int32_t parent;
std::cin >> parent >> arr[i];
parent--;
if(parent == -1)
root = i;
else
tree[parent].push_back(i);
}
int32_t* subtree_sizes = new int32_t[n];
dfs2(root, tree, subtree_sizes);
int32_t** dp = new int32_t*[n];
dfs(root, tree, dp, arr, subtree_sizes);
std::cout << dp[root][m];
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |