(This problem is a variant of LeetCode 1557 – Minimum Number of Vertices to Reach All Nodes. Completing that problem first is recommended.)
You are given a directed network of n servers, labeled 0 through n - 1. The connections are provided as a list of directed edges, where each edges[i] = [u, v] indicates a directed link from server u to server v that allows a signal to travel from u to v.
Your goal is to determine the minimum number of servers that must be activated initially so that every server in the network eventually receives a signal. A server can receive a signal either by being activated directly, or by receiving it through a sequence of directed connections from an already‑activated server.
The network may contain cycles and disconnected components. Return the smallest number of initial activations required to make all servers reachable.
Constraints:
0 ≤ edges.length ≤ 10^5Example 1:
n = 5, edges = [[0, 1], [1, 2], [2, 0], [3, 4]]
2
Servers 0, 1, and 2 form a cycle, so activating any one of them reaches all three. Servers 3 and 4 are in a separate chain, so activating server 3 covers both. Hence, a minimum of 2 initial servers is needed.
Example 2:
n = 6, edges = [[0, 1], [1, 0], [2, 1], [2, 5], [5, 4], [1, 3]]
1
Example 3:
n = 6, edges = [[0, 1], [1, 0], [2, 1], [1, 3], [5, 4]]
218:T839,
Example 1
Input:
5
4
0 1
1 2
2 0
3 4
Output: 2
Example 2
Input:
6
6
0 1
1 0
2 1
2 5
5 4
1 3
Output: 1