Table of Contents

ExplanationImplementation

Explanation

Let's define best[n][k]\texttt{best}[n][k] as the smallest edge set that connects a subtree of size kk rooted at node nn to the rest of the tree (It's undefined if such a subtree is impossible). The base cases for any node are as follows:

best[n][0]={}\texttt{best}[n][0] = \{\}
best[n][1]={}\texttt{best}[n][1] = \{\}

Then, when we're processing a specific node in the tree, we go through each of its children and incorporate the child's DP array into that of the current node. For each of these subtrees, we have 2 choices:

  1. We don't include any of it, and add the edge connecting it and the current node to the current edge set we're processing.
  2. We include some of it, and with that add the edge set stored in that subtree's DP array.

After that, we can simply go through each node and get the overall minimum edge set to get a city of size kk.

Implementation

C++

#include <algorithm>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
using std::cout;
using std::endl;
using std::pair;
using std::vector;

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!