INOI2203B - Editorial

Problem Link - Diocletian

Problem Statement

Chef has been promoted to the position of Emperor of Siruseri!

Unfortunately, he presides over an empire in crisis. With marauders encroaching on all sides, he decides to follow the lead of the ancient Romans and split the empire in two.

Siruseri has n cities and m train lines, with each train line i connecting two cities u_i and v_i in both directions. It is guaranteed that Siruseri is connected, meaning that it is possible to travel from any city to any other city using the train lines, and that all train lines are distinct.

Chef wants to split Siruseri into two countries, Alfa and Bravo. To do this, he first assigns city 1, as the capital, to Alfa. Then, he assigns each of the remaining cities to one of the two countries. Each country will take control of all train lines that connect two cities which are both part of the country. That is, Alfa will take all lines which connect two Alfa cities, and Bravo will take all lines which connect two Bravo cities.

Chef considers a plan for splitting Siruseri reasonable if both Alfa and Bravo are connected. That is, it is possible to travel from any Alfa city to any other Alfa city using only Alfa train lines, and similarly for Bravo.

Chef has enlisted your help to provide him with plans. Since he wants a variety of choices, he requests you give at least p distinct reasonable plans.

Approach

To approach the problem, we aim to partition the cities into two connected countries, Alfa and Bravo, starting with the capital ( city 1) assigned to Alfa. By using a Depth-First Search (DFS), we can recursively assign each city to either Alfa or Bravo while maintaining the connectivity of both countries. This method ensures a valid split and verifies the connectivity of the cities within each country.

If the number of train lines equals the number of cities minus one, we perform a DFS to ensure both groups are connected. If there are more train lines, we use backtracking to explore different combinations of assignments to Alfa and Bravo. For each valid assignment, we check if both countries are connected. We continue until we find at least p distinct reasonable plans.

This process ensures that we explore all possible assignments of cities, making sure that both Alfa and Bravo are connected, and count those that meet the required condition.

Time Complexity

The time complexity is O(m \times 2^n), where m is the number of train lines and n is the number of cities, due to the DFS traversal and recursive generation of city assignments.

Space Complexity

The space complexity is O(n + m) for storing the graph and the recursive call stack.