Tap here to switch tabs
Problem
Submissions

Problem: Count Visited Nodes in a Directed Graph

Try to solve the Count Visited Nodes in a Directed Graph problem.

Statement

There is a directed graph consisting of n nodes labeled from 00 to n −1- 1. Each node i has exactly one outgoing directed edge to edges[i].

Starting from a node i, the process moves along the graph by continually following the outgoing edges until a node that has already been visited during the current traversal is reached.

Return an array ans of size n, where ans[i] is the total number of distinct nodes visited when beginning the traversal from node i.

Constraints:

  • n ==== edges.length

  • 2≤2 \leq n ≤105\leq 10^5

  • 0≤0 \leq edges[i] ≤\leq n −1- 1

  • edges[i] ≠\neq i

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Count Visited Nodes in a Directed Graph

Try to solve the Count Visited Nodes in a Directed Graph problem.

Statement

There is a directed graph consisting of n nodes labeled from 00 to n −1- 1. Each node i has exactly one outgoing directed edge to edges[i].

Starting from a node i, the process moves along the graph by continually following the outgoing edges until a node that has already been visited during the current traversal is reached.

Return an array ans of size n, where ans[i] is the total number of distinct nodes visited when beginning the traversal from node i.

Constraints:

  • n ==== edges.length

  • 2≤2 \leq n ≤105\leq 10^5

  • 0≤0 \leq edges[i] ≤\leq n −1- 1

  • edges[i] ≠\neq i