Graph Theory37 sections · 1633 units
Open in Course

Planets and Kingdoms - Approach

(SCC detection)

The approach is direct: run Kosaraju or Tarjan to find all SCCs in the graph. The number of SCCs equals the minimum number of kingdoms. Why?

Each SCC is a maximal group of mutually reachable nodes, so you cannot combine two different SCCs into one kingdom. Assign each node the ID of its SCC (a number from 11 to the number of SCCs). Print the total SCC count first, then print each node's SCC ID in order from node 11 to node nn.