Village Road Network - Problem
There are n villages numbered from 0 to n - 1. You are given a 2D array roads where each roads[i] = [u, v] means there is a bidirectional road between village u and village v.
Given two integers source and destination, return true if a traveler can reach destination from source using the available roads, or false otherwise.
Input & Output
Example 1 — Disconnected Villages
$
Input:
n = 6, roads = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
›
Output:
false
💡 Note:
Villages 0,1,2 form one cluster. Villages 3,4,5 form another. No road connects the two clusters.
Example 2 — Connected Villages
$
Input:
n = 3, roads = [[0,1],[1,2],[2,0]], source = 0, destination = 2
›
Output:
true
💡 Note:
All villages are connected. Route: 0 → 1 → 2 or directly 0 → 2.
Example 3 — Same Village
$
Input:
n = 1, roads = [], source = 0, destination = 0
›
Output:
true
💡 Note:
Source equals destination — the traveler is already there.
Constraints
- 1 ≤ n ≤ 2 × 10^5
- 0 ≤ edges.length ≤ 2 × 10^5
- edges[i].length == 2
- 0 ≤ u, v < n
- 0 ≤ source, destination < n
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code