First let’s minimize the answer. The key observation is that at most one pair passes through the edge (a,b). This is because if two or more pair pass that edge, we can pair two vertices in the same side of that edge and get a better answer.
Furthermore, the number of pairs that pass through (a,b) is camod2, where ca the size of the component on a’s side.
For the maximized answer, the strategy is similar. The observation is that nodes of one component are paired with node of the other component. We can do the reversed thing in the minimized answer to prove this. Thus, each edge is counted min(ca,cb) times.
Both the maximized answer and the minimized answer can be calculated at the same time in one DFS.