F - Keep Connect Editorial by en_translator
We consider finding the answer with DP (Dynamic Programming).
Let us call the edges as follows:
- Edge \(a_i\) denotes the edge connecting Vertices \(i\) and \(i+1\), for \(1\leq i\leq N-1\).
- Edge \(b_i\) denotes the edge connecting Vertices \(i+1\) and \(N+i+1\), for \(1\leq i\leq N-1\).
- Edge \(c_i\) denotes the edge connecting Vertices \(i+1\) and \(N+i+1\), for \(0\leq i\leq N-1\).
Here, for each \(k=0,1,\ldots,N-1\), consider a subgraph \(G_i\) consisting of \((2k+2)\) vertices, Vertices \(1,2,\ldots,k+1\) and \(N+1,N+2,\ldots,N+k+1\), where it has been determined whether or not Edge \(a_i\), \(b_i\), and \(c_i\) is removed or not. Note that \(G=G_{N-1}\).
Here, if \(G_k\) has an (non-empty) connected component without Vertex \(k+1\) and \(N+k+1\), then we cannot make the resulting graph connected even if we retain all the Edges \(a_i\), \(b_i\), \(c_i\) for \(i> k\). Thus, there are only two kinds of possible subgraph \(G_k\) ending up in a connected \(G\):
- State \(0\): \(G_k\) is connected;
- State \(1\): \(G_k\) has exactly two connected components; one containing Vertex \(k+1\) and the other containing Vertex \(N+k+1\).
So, let us consider updating the table \(dp[i][j][k]\) to fill it with the number, modulo \(P\), of graphs in State \(k\) which lacks exactly \(j\) of edges \(a_x\), \(b_x\), \(c_x\) for \(x\leq i\). What we want is the value of \(dp[N-1][j][0]\) for each \(1\leq j\leq N-1\).
First, for \(i=0\), \(G_0\) consists of two vertices. The only choice is whether \(c_0\) is removed or not, so \(dp[0][0][0]=dp[0][1][1]=1\) and \(dp[0][j][k]=0\) for any other indices.
In the update, when obtaining \(G_i\) from \(G_{i-1}\), three more additional Edges \(a_i\), \(b_i\) and \(c_i\) have to be taken into account. For a graph \(G_{i-1}\) in each of State \(0\) and \(1\), we consider how it affects to retain or remove these edges.
- When \(G_{i-1}\) is in State \(0\)
There are three connected components: \(G_{i-1}\), Vertex \(i+1\), and \(N+i+1\). There are Edge \(a_i\) between \(G_{i-1}\) and Vertex \(i+1\), Edge \(b_i\) between \(G_{i-1}\) and Vertex \(N+i+1\), and Edge \(c_i\) between Vertex \(i+1\) and \(N+i+1\).
First, if \(a_i\) and \(b_i\) are both removed, in the resulting \(G_i\), \(G_{i-1}\) will become a connected component containing neither Vertex \(i+1\) nor \(N+i+1\), and they will never become connected. Otherwise, \(G_i\) will become either in State \(0\) or State \(1\). Specifically, \(G_i\) will become entirely connected if and only if two or more of Edges \(a_i\), \(b_i\), and \(c_i\) are retained, resulting in State \(0\); otherwise, it will turn into State \(1\).
- When \(G_{i-1}\) is in State \(1\)
There are four connected component:, a connected component \(C_1\) of \(G_{i-1}\) containing Vertex \(i\), another connected component \(C_2\) of \(G_{i-1}\) containing Vertex \(N+i\), Vertex \(i+1\), and Vertex \(N+i+1\). There are Edge \(a_i\) between \(C_1\) and Vertex \(i+1\), Edge \(b_i\) between \(C_2\) and Vertex \(N+i+1\), and Edge \(c_i\) between Vertex \(i+1\) and \(N+i+1\).
First, if at least one of Edge \(a_i\) and Edge \(b_i\) is removed, the connected component in \(G_{i-1}\) containing the endpoint of the removed edge becomes a connected component containing neither Vertices \(i+1\) nor \(N+i+1\), which never ends up in being a single connected component. Otherwise, \(G_i\) will become either in State \(0\) or State \(1\). Specifically, \(G_i\) will become entirely connected if all of the three edges including \(c_i\), resulting in State \(0\); otherwise, it will remain State \(1\).
Now we have explained all the transitions. This can be implemented as a distributing DP, where each update for \(dp[i][j][k]\) can be done in a constant time, \(O(1)\).
Therefore, the total time complexity is \(O(N^2)\), which is fast enough.
Note that the transition above can be handled without manual checkups but with a computational simulation. Also, we can use the fact that \(dp[i][j][k]=0\) if \(j\geq i+2\), since there are always at most two connected components, in order to reduce the constant factor of the computation.
posted:
last update: