Submission #474246

#TimeUTC-0UsernameProblemLanguageResultExecution timeMemory
4742462021-09-17 13:47:07hoanghq2004Magic Tree (CEOI19_magictree)C++14
100 / 100
141 ms32680 KiB
/*
Central European Olympiad in Informatics 2019 (Day 2)
Using dynamic programming: dp[u][t] is the maximum of fruits can be harvested at time point earlier than or equal t
Formula: dp[u][t] = sum(dp[v][t]) (if t < t[u])
= max(sum(dp[v][t]), sum(dp[v][t[u]]) + w[u]) otherwise
So easy to find that: dp[u][t] is an non-decreasing array with t = 0...k and the number of distinct values will smaller than or equal the number of vertex in the subtree rooted u + 1
and the values only can be increase if t = t[v]
Optimize:
- map <int, long long> opt[u] contains all values of dp[u][i] - dp[u][i - 1]
- merge small to large so the complexity is O(n * log^2(n))
- for each subtree rooted u, dp[u][t[u]] += w[u], so opt[u][t[u]] += w and the values after t[u] have to be increase that: dp[u][t] = max(dp[u][t], w[u]).
here, define cur = w[u] - dp[u][t] with t = t[u]...
while opt[u][t] < w[u], dp[u][t] is smaller than w[u], so we should assign dp[u][t] = w[u]
subtract opt[u][t] from cur and assign opt[u][t] = 0 (erase from map)
in other case, dp[u][t] is larger than w[u] then all values from t shouldn't be changed, subtract w[u] from opt[u][t]
*/
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
vector <int> e[100010];
int w[100010], t[100010];
map <int, long long> opt[100010];
long long res;
void dfs(int u) {
for (auto v: e[u]) {
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...