Skip to content

Practice Count Visited Nodes in a Directed Graph

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2876
LeetCode Problem

Count Visited Nodes in a Directed Graph

Problem Statement

The directed graph has one outgoing edge from each node i to edges[i]. Starting separately from each node, repeatedly follow outgoing edges until the next node has already been visited in that walk. Return an array containing the number of distinct visited nodes for each start, including the starting node.

Source: Count Visited Nodes in a Directed Graph on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [1,2,0,0]
Output
[3,3,3,4]

# Example 2

Input
edges = [1,2,3,4,0]
Output
[5,5,5,5,5]

Constraints

n == edges.length
2 <= n <= 10^5
0 <= edges[i] <= n - 1
edges[i] != i

Related LeetCode Problems

Practice more problems similar to Count Visited Nodes in a Directed Graph.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.