Count Visited Nodes in a Directed Graph - Problem

Imagine exploring a maze of one-way paths where each room has exactly one exit leading to another room. You're given a directed graph with n nodes numbered from 0 to n-1, where each node i has exactly one outgoing edge to node edges[i].

Your task is to simulate a traversal process starting from each node: keep following the edges until you revisit a node you've already seen in this journey. Count how many different nodes you visit during this process (including the starting node, but not counting the final revisited node).

Goal: Return an array where answer[i] represents the number of unique nodes visited when starting the traversal from node i.

Example: If you start at node 0 and follow the path 0→1→2→1, you visit nodes {0, 1, 2} before revisiting node 1, so the answer is 3.

Input & Output

example_1.py — Simple Path with Cycle
$ Input: edges = [1,2,0,0]
Output: [3,3,3,4]
💡 Note: Starting from node 0: 0→1→2→0 (revisit 0), visited {0,1,2} = 3 nodes. Starting from node 1: 1→2→0→1 (revisit 1), visited {1,2,0} = 3 nodes. Starting from node 2: 2→0→1→2 (revisit 2), visited {2,0,1} = 3 nodes. Starting from node 3: 3→0→1→2→0 (revisit 0), visited {3,0,1,2} = 4 nodes.
example_2.py — Self Loop
$ Input: edges = [1,0,0]
Output: [2,2,3]
💡 Note: Starting from node 0: 0→1→0 (revisit 0), visited {0,1} = 2 nodes. Starting from node 1: 1→0→1 (revisit 1), visited {1,0} = 2 nodes. Starting from node 2: 2→0→1→0 (revisit 0), visited {2,0,1} = 3 nodes.
example_3.py — Single Node Cycle
$ Input: edges = [0]
Output: [1]
💡 Note: Starting from node 0: 0→0 (immediately revisit 0), visited {0} = 1 node.

Visualization

Tap to expand
🏰 Castle Room Explorer - Cycle Detection🚪 0Count: 4🚪 1Count: 3🚪 2Count: 2🚪 3Count: 2🔄 Cycle Detected!Rooms 1→2→3→1 (Length: 2)💡 Memoization saves time by reusing computed results
Understanding the Visualization
1
Start Exploration
Begin exploring from a room, marking our path
2
Track the Journey
Keep following doors and recording visited rooms
3
Detect the Loop
When we revisit a room, we've found our cycle
4
Calculate Results
Rooms in the cycle get cycle length, others add their distance
5
Memoize & Reuse
Store results to avoid re-exploring the same paths
Key Takeaway
🎯 Key Insight: By detecting cycles and using memoization, we solve this in O(n) time instead of O(n²), making it efficient even for large castles!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each node is visited at most twice - once for cycle detection and once for result computation

n
2n
Linear Growth
Space Complexity
O(n)

We need space for recursion stack, memoization array, and cycle detection

n
2n
Linearithmic Space

Constraints

  • n == edges.length
  • 1 ≤ n ≤ 105
  • 0 ≤ edges[i] ≤ n - 1
  • edges[i] ≠ i for all i
  • Each node has exactly one outgoing edge
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen