Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
#include
#include
#include
using namespace std; const int maxn = 3e5 + 10; vector
graph[maxn]; int rating[maxn]; int parent[maxn]; int dp[maxn]; int dfs(int node) { if(dp[node] != -1) { return dp[node]; } int taken = rating[node]; for(int i = 0; i < (int) graph[node].size(); i++) { int neighbour = graph[node][i]; for(int j = 0; j < (int) graph[neighbour].size(); j++) { int neighbour_of_neighbour = graph[neighbour][j]; taken += dfs(neighbour_of_neighbour); } } int not_taken = 0; for(int i = 0; i < (int) graph[node].size(); i++) { int neighbour = graph[node][i]; not_taken += dfs(neighbour); } return dp[node] = max(taken, not_taken); } int main() { ios_base::sync_with_stdio(false); memset(dp, -1, sizeof dp); int n; cin >> n; cin >> rating[0]; for(int i = 1; i < n; i++) { int p; cin >> rating[i] >> p; parent[i] = p; graph[p].push_back(i); } cout << dfs(0) << endl; return 0; } /* 7 1 2 1 3 2 4 2 5 3 6 3 7 **/
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)