Is There a Road
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
0 <= len(roads) <= 30- towns are numbered
0to19, and a road joins two different towns a != b
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Graphs problems
- Next DoorBeginner
- No HandshakeBeginner
- Who Points at MeBeginner
- Busiest NodeEasy
- Cable GroupsEasy
- Count the LakesEasy
- Everything Needed FirstEasy
- Everything ReachableEasy