# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
659886 | 600Mihnea | Magic Tree (CEOI19_magictree) | C++17 | 2086 ms | 23400 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.
bool home = 0;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int m;
int k;
int p[N];
int type[N];
int w[N];
int sub[N];
int bg[N];
ll dp[N];
int low[N];
int high[N];
int ord[N];
int top;
vector<int> g[N];
int Counter = 0;
int last_seen[N];
void bu(int a) {
low[a] = ++top;
ord[top] = a;
sub[a] = 1;
for (auto &b : g[a]) {
bu(b);
sub[a] += sub[b];
if (sub[b] > sub[bg[a]]) {
bg[a] = b;
}
}
high[a] = top;
}
vector<pair<int, ll>> get_and_clr_dp(int v) {
Counter++;
vector<pair<int, ll>> sol;
for (int j = low[v]; j <= high[v]; j++) {
if (int i = type[ord[j]]) {
if (last_seen[i] == Counter) {
continue;
}
last_seen[i] = Counter;
sol.push_back({i, dp[i] - dp[i - 1]});
}
}
for (int i = 1; i <= k; i++) {
dp[i] = 0;
}
return sol;
}
ll dpaux[N];
void solve(int a) {
if (g[a].empty()) {
if (type[a]) {
for (int j = type[a]; j <= k; j++) {
dp[j] += w[a];
}
}
} else {
assert(bg[a] != -1);
vector<vector<pair<int, ll>>> all;
for (auto &b : g[a]) {
if (b != bg[a]) {
solve(b);
all.push_back(get_and_clr_dp(b));
continue;
}
}
{
int b = bg[a];
solve(b);
for (auto &v : all) {
for (auto &g : v) {
for (int j = g.first; j <= k; j++) {
dp[j] += g.second;
}
}
}
if (type[a]) {
dp[type[a]] += w[a];
for (int j = type[a]; j <= k; j++) {
dp[j] = max(dp[j], dp[j - 1]);
}
}
}
}
}
int main() {
if (!home) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
} else {
freopen("input.txt", "r", stdin);
}
cin >> n >> m >> k;
if (0) {
cout << " : ";
for (int i = 1; i <= k; i++) {
cout << i << " ";
}
cout << "\n";
}
for (int i = 2; i <= n; i++) {
cin >> p[i];
g[p[i]].push_back(i);
}
for (int i = 1; i <= m; i++) {
int v;
cin >> v;
cin >> type[v];
cin >> w[v];
}
{
map<int, int> mp;
for (int i = 1; i <= n; i++) {
if (type[i]) {
mp[type[i]] = 0;
}
}
int cr = 0;
for (auto &it : mp) {
it.second = ++cr;
}
for (int i = 1; i <= n; i++) {
if (type[i]) {
type[i] = mp[type[i]];
}
}
k = min(k, m);
}
bu(1);
solve(1);
cout << dp[k] << "\n";
return 0;
}
/**
dp[vertex][last_value] = ?
**/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |