MAXTOPO - Editorial

I used the idea in this blog post to solve it for K \in {1,2,3, \ldots, N}. The idea is that the number of topological orderings, if the root is v, can be simply expressed as \frac{N!}{\prod_{u=1}^N s^{(v)}_u}, where s^{(v)}_u is the size of the subtree rooted at u. To find the root with the maximum number of orderings, you just have to find the minimum of the product, i.e., \min_v \prod_{u=1}^N s^{(v)}_u. This product can be large but you can take the logarithm. See this solution. The sorting in line 56 is an overkill for K \in {1,2}, but then in the next line we can choose any K.

5 Likes