PIXELBANKv8.2.1
Menu

Find if Path Exists in Graph

Given n nodes, undirected edges, and two nodes source and destination, return True if a path exists between them.

Input: first line = n, second = edges as u:v comma-separated (or 'none'), third = source destination.

Example:

Input:
3
0:1,1:2,2:0
0 2
Output:
True
Reasoning:
  • The graph is constructed with n=3n=3 nodes and undirected edges between nodes 0 and 1, 1 and 2, and 2 and 0.
  • The edges create a cycle: 01200 \leftrightarrow 1 \leftrightarrow 2 \leftrightarrow 0, allowing for a path between any two nodes.
  • A path exists from the source node 0 to the destination node 2, as they are directly connected through node 1 and also through the cycle 020 \leftrightarrow 2.
  • The function returns True because a path is found between the source and destination nodes.

Constraints:

  • 1 <= n <= 2 * 10^5
  • 0 <= edges.length <= 2 * 10^5
Editor

Test Results

0/0
Run code to see test results.