FunctionRacerProblemsRace

Is There a Road

BeginnerGraphsPython, JavaScript, TypeScript, Java, C++

Towns are numbered from 0. Each entry of roads is a pair [x, y]: a road between town x and town y that can be driven in both directions.

Return True if a single road joins town a and town b directly, and False otherwise. Getting there through other towns does not count.

Example 1

roads [[0,1],[2,0],[3,2]]
a 0
b 2
returns true

The road [2, 0] joins towns 0 and 2. It does not matter which town is written first.

Example 2

roads [[0,2],[2,1]]
a 0
b 1
returns false

Town 1 can be reached from 0 through town 2, but no road joins them directly.

Example 3

roads []
a 0
b 1
returns false

There are no roads at all.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems