公式

C - K Spanning Tree 解説 by evima


Let \(x\) be the weight of the minimum spanning tree of \(G\), and \(y\) be the weight of the maximum spanning tree. A solution exists if \(x \le K \le y\), and the answer is -1 otherwise. We show this below.

It is clear that the answer is -1 if \(x \le K \le y\) is not satisfied.

If \(x \le K \le y\) is satisfied, a spanning tree satisfying the requirement can be constructed by the following algorithm.

  1. Consider the subgraph composed only of weight-\(0\) edges, and find its connected components. (At this point, the weight-\(0\) edges are not yet added to the solution.) Connect these connected components to each other using weight-\(1\) edges without forming a cycle. The number of edges used here is \(x\).
  2. Continue using weight-\(1\) edges without forming a cycle, until the number of weight-\(1\) edges used reaches \(K\). This is possible since \(K \le y\), because maximally extending a forest composed only of weight-\(1\) edges allows using \(y\) edges.
  3. For the rest, use weight-\(0\) edges without forming a cycle. By step 1, the graph formed by combining the chosen weight-\(1\) edges with all weight-\(0\) edges is connected, so it can be turned into a spanning tree.

Kruskal’s algorithm can be used for finding \(x\) and \(y\), and BFS or Union-Find can be used for managing connected components in the spanning tree construction algorithm.

投稿日時:
最終更新: