Minimum Runes to Add to Cast Spell Problem
Description
LeetCode Problem 3383.
Alice has just graduated from wizard school, and wishes to cast a magic spell to celebrate. The magic spell contains certain focus points where magic needs to be concentrated, and some of these focus points contain magic crystals which serve as the spell's energy source. Focus points can be linked through directed runes, which channel magic flow from one focus point to another.
You are given a integer n denoting the number of focus points and an array of integers crystals where crystals[i] indicates a focus point which holds a magic crystal. You are also given two integer arrays flowFrom and flowTo, which represent the existing directed runes. The i^th rune allows magic to freely flow from focus point flowFrom[i] to focus point flowTo[i].
You need to find the number of directed runes Alice must add to her spell, such that each focus point either:
- Contains a magic crystal.
- Receives magic flow from another focus point.
Return the minimum number of directed runes that she should add.
Example 1:
Input: n = 6, crystals = [0], flowFrom = [0,1,2,3], flowTo = [1,2,3,0]
Output: 2
Explanation:

Add two directed runes:
- From focus point 0 to focus point 4.
- From focus point 0 to focus point 5.
Example 2:
Input: n = 7, crystals = [3,5], flowFrom = [0,1,2,3,5], flowTo = [1,2,0,4,6]
Output: 1
Explanation:

Add a directed rune from focus point 4 to focus point 2.
Constraints:
2 <= n <= 10^51 <= crystals.length <= n0 <= crystals[i] <= n - 11 <= flowFrom.length == flowTo.length <= min(2 * 10^5, (n * (n - 1)) / 2)0 <= flowFrom[i], flowTo[i] <= n - 1flowFrom[i] != flowTo[i]- All pre-existing directed runes are distinct.
Solution
Approach: BFS + topological order of the remaining graph
First mark everything reachable from a crystal with a BFS; those focus points are already fine.
The unmarked points need help. Run a DFS over them and record the finishing order; reversed, it lists the points so that a point comes before the ones it can reach (a topological order of the strongly connected components). Go through this order: whenever a point is still not covered, it is the source of a component nobody covers, so add one rune into it, mark everything it reaches with a BFS, and count it.
Complexity: Time O(n + m), space O(n + m), where m is the number of runes.
from collections import deque
class Solution:
def minRunesToAdd(
self, n: int, crystals: List[int], flowFrom: List[int], flowTo: List[int]
) -> int:
def bfs(q: Deque[int]):
while q:
a = q.popleft()
for b in g[a]:
if vis[b] == 1:
continue
vis[b] = 1
q.append(b)
def dfs(a: int):
vis[a] = 2
for b in g[a]:
if vis[b] > 0:
continue
dfs(b)
seq.append(a)
g = [[] for _ in range(n)]
for a, b in zip(flowFrom, flowTo):
g[a].append(b)
q = deque(crystals)
vis = [0] * n
for x in crystals:
vis[x] = 1
bfs(q)
seq = []
for i in range(n):
if vis[i] == 0:
dfs(i)
seq.reverse()
ans = 0
for i in seq:
if vis[i] == 2:
q = deque([i])
vis[i] = 1
bfs(q)
ans += 1
return ans