Loading...
Loading...
You are given an integer n and a list deps. There are n services labeled 0 to n - 1. Each element in deps contains [a, b] meaning service a depends on service b.
A service that depends on b starts automatically as soon as b is running. If a service depends on several others, any one of them running is enough. Automatic starts chain, so a single start can bring up many services.
You can also start any service manually.
Return the minimum number of manual starts needed so that every service ends up running.
Note that deps may contain the same pair more than once.
Input: n = 5
deps = [[1, 0],
[2, 0],
[3, 4]]
Output: 2
Explanation: Starting service 0 starts services 1 and 2. Starting service 4 starts service 3.
Nothing starts 0 or 4 automatically, so both need a manual start.
Input: n = 4
deps = [[0, 1],
[1, 0],
[2, 1]]
Output: 2
Explanation: Services 0 and 1 depend on each other, so starting either one starts both, and then service 2.
Service 3 depends on nothing and nothing depends on it, so it needs its own manual start.
Input: n = 3
deps = []
Output: 3
Explanation: There are no dependencies, so every service must be started manually.
deps.length ≤2⋅105[a, b] in deps