Official

D - Sum of SCC Editorial by evima


The number of strongly connected components in a directed graph \(G\) that is a tournament equals the following value minus \(1\).

  • The number of ways to divide the vertex set \(\lbrace 1,2,\dots,N \rbrace\) of \(G\) into two vertex sets \(A\) and \(B\) that satisfy the following condition:
    • every edge between a vertex in \(A\) and vertex in \(B\) is directed from \(A\) to \(B\).

This can be seen from the following fact: let \(s_1,s_2,\dots,s_k\) be the strongly connected components of \(G\) (with smaller numbers being upstream), and the condition is satisfied by the \(k+1\) pairs \(A=\lbrace s_1,s_2,\dots,s_i \rbrace,B=\lbrace s_{i+1},s_{i+2},\dots,s_k \rbrace\) for integers \(i\) such that \(0 \leu i \le k\).

Now, we can perform the following dynamic programming.

\(dp[n][i][j] :=\) The number of pairs of an \(n\)-vertex tournament graph \(G\) and vertex sets \(A,B\) such that the size of \(A\) is \(i\) and there are \(j\) edges directed from a smaller vertex number to a larger vertex number.

The update from \(dp[n][i][j]\) is performed as follows.

  • When adding vertex \(n+1\) to \(A\):
    • The direction of edges between vertices in \(A\) can be freely determined.
    • Edges to vertices in \(B\) must be directed from \(i+1\).
    • Therefore, for \(0 \le k \le i\), add \(dp[n][i][j] \times \binom{i}{k}\) to \(dp[n+1][i+1][j+k]\).
  • When adding vertex \(n+1\) to \(B\):
    • Edges to vertices in \(A\) must be directed to \(i+1\).
    • The direction of edges between vertices in \(B\) can be freely determined.
    • Therefore, for \(0 \le k \le n-i\), add \(dp[n][i][j] \times \binom{n-i}{k}\) to \(dp[n+1][i][j+i+k]\).

Therefore, by performing dynamic programming with \(\mathrm{O}(N^4)\) states and \(\mathrm{O}(N)\) transitions from each state, this problem can be solved in \(\mathrm{O}(N^5)\). Also, if the constant factor is not bad, \(\mathrm{O}(N^5\log N)\), \(\mathrm{O}(N^6)\), etc., will get AC.

(Modified the first draft written by GPT-4.)

posted:
last update: