AC - Select Edges
Author: Justin Ji
Explanation
To solve this problem, we write a tree DP. Consider that the only factor we need to differentiate nodes on is whether or not we can connect a given node to its parent. Thus, our DP state is the following:
- is the best result in the subtree of if we can connect to its parent
- is the best result in the subtree of if we can't connect to its parent
For a given node , the "gain" we get from adding this node in is:
Thus, we want to use our allowed edges on the nodes that have the most benefit when adding an edge to them. Note that we handle the cases for and pretty similarly.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;int main() {int n;cin >> n;vector<int> d(n);
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!