Ctrl K

Will the Message Get Through

Easy
An office has n computers numbered 0 to n - 1. Some pairs are wired together: each links[i] = [u, v] is a cable between computers u and v that carries data both ways. Any two computers share at most one cable, and no cable loops back to the same computer. Return True if a message can get from computer start to computer target, passing along cables through any computers in between, and False otherwise.
Example 1
n=5, links=[[0, 3], [3, 4], [1, 2]], start=0, target=4 → True

A message travels along 0 → 3 → 4.

Example 2
n=5, links=[[0, 3], [3, 4], [1, 2]], start=4, target=2 → False

Computers 0, 3, 4 and computers 1, 2 form two separate groups with no cable between them.

Example 3
n=2, links=[], start=1, target=1 → True

The start and the target are the same computer: the path is empty, but it exists.

Constraints
  • 1 ≤ n ≤ 2·10⁵
  • 0 ≤ len(links) ≤ 2·10⁵
  • links[i] = [u, v], 0 ≤ u, v ≤ n - 1, u ≠ v
  • No cable appears twice
  • 0 ≤ start, target ≤ n - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDo the Links Form a Tree MediumIsolated Groups in a Network MediumRatio Chain Queries MediumClusters of Linked Servers MediumThe Extra Cable Medium
loading Python…
Input
n=5, links=[[0, 3], [3, 4], [1, 2]], start=0, target=4
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 7 tests on the server, hidden ones included.