公式

A - Meeting on Tree 解説 by evima 印刷ページ


Fix the set of squirrels to be chosen. The following procedure correctly determines where to hold the meeting.

  • For each edge of the tree, orient it toward the endpoint that gives the smaller cost when chosen as the meeting location. If the costs are equal, leave the edge unoriented.
  • Take vertex \(1\) as the provisional meeting location. As long as there is a directed edge leaving the provisional meeting location, follow that edge and make its other endpoint the new provisional meeting location.
  • Choose the final provisional meeting location as the meeting location.

Each edge points toward the connected component containing (strictly) more squirrels after that edge is removed. No vertex has two or more outgoing directed edges. Otherwise, there would be two or more mutually disjoint sets of chosen squirrels, each containing a majority of all chosen squirrels, which is a contradiction. Also, the set of undirected edges is connected. Thus, the procedure above greedily and monotonically reduces the cost starting from vertex \(1\), and stops at a vertex with minimum cost.

Therefore, the minimum cost can be decomposed into the following sum.

  • \((\) cost at vertex \(1\) \()-\displaystyle\sum_{e}(e\)’s cost reduction when traversed \()\)

Let us compute the sum over all choices of squirrels for each term.

The first term is easy. Let \(d_v\) be the distance between vertices \(v\) and \(1\). The sum of the costs when vertex \(1\) is chosen as the meeting location is \(\sum_{v}2^{N-1}d_v\).

We compute the second term separately for each edge. Consider a directed edge \(u\to v\) pointing away from the root. Let the numbers of chosen squirrels in the connected components on the \(u,v\) sides after removing this edge be \(a,b\), respectively. The condition for the edge to be oriented this way is \(a\lt b\). If this holds, the provisional meeting location in the procedure above necessarily follows this edge, reducing the cost by \(b-a\). Thus, if the connected component on the \(v\) side has size \(s\), this edge’s contribution is

\[ \begin{aligned} &\ \sum_{0\le a\lt b} \binom{s}{b}\binom{N-s}{a}(b-a)\\ &= \sum_{c\ge 1}\sum_{a\ge 0}\binom{s}{a+c}\binom{N-s}{a}c\\ &= \sum_{c\ge 1}c\sum_{a\ge 0}\binom{s}{s-a-c}\binom{N-s}{a}\\ &= \sum_{c\ge 1}c\binom{N}{s-c}\\ &= \sum_{d=0}^{s}(s-d)\binom{N}{d}\\ &= [x^s]\dfrac{x(1+x)^N}{(1-x)^2} \end{aligned} \]

Here, we used Vandermonde’s convolution along the way. These values can be computed for all \(s\) by taking prefix sums of \(\binom{N}{i}\) twice.

This gives the following algorithm.

  1. Root the tree at any vertex.
  2. Let \(A\) be the sum of the distances from the root to all vertices.
  3. For each subtree rooted at a non-root vertex, let its size be \(s\), and let \(B\) be the sum of the values above for these subtrees.
  4. The answer is \(2^{N-1}A-B\).

This algorithm runs in \(O(N)\) time.

投稿日時:
最終更新: