Problem Description
You are given n rooms (numbered 0 to n-1) and an expansion plan represented by the array prevRoom where prevRoom[i] indicates that room i can be built only after its previous room prevRoom[i] is built; room 0 is already built (prevRoom[0] = -1). All rooms will eventually be reachable from room 0. You are allowed to build one room at a time (provided its predecessor is built) and can only travel between rooms that are already built and directly connected. Determine the number of valid orders in which you can build all the rooms, modulo 10^9 + 7.
Key Insights
- The dependency structure forms a tree rooted at room 0.
- The order in which rooms are built corresponds to a topological ordering that respects the tree structure.
- The problem reduces to counting the number of ways to interleave the build orders of subtrees.
- Use combinatorics (multinomial coefficients) to determine how many ways to merge orders of independent subtrees.
- Precomputing factorials and modular inverses facilitates efficient combination calculations.
Space and Time Complexity
Time Complexity: O(n), where n is the number of rooms (each node is processed once and factorials are computed in O(n)). Space Complexity: O(n), due to the recursion stack (tree traversal) and additional arrays for factorials and tree structure.
Solution
We first convert the expansion plan into a tree representation. For each room (node), we perform a DFS to compute two things: the size of the subtree and the number of ways to build the subtree. For a node, the number of ways to interleave the build orders from its children is given by the multinomial coefficient:
total_ways = (factorial(total_children_count) / (product of factorial(each_child_subtree_size))) * (product of ways for each child)
Precompute factorials and inverse factorials modulo 10^9+7 to efficiently calculate combinations. By recursively combining results from child subtrees, we obtain the number of valid build orders for the entire tree.