← 返回 bloomberg 的题目列表Check Connectivity Between Two Subway Stations
类型:online_judge
You are given a New York subway network with stations (A, B, C, D, E, ...) and connectivity information: for each station, which other stations it connects to (an adjacency list).
Implement a function isConnected(start, end) that returns whether two given stations start and end are connected, i.e., whether there exists a path from start to end.
Notes:
Stations may be represented as strings or integers.
Typically, “connected” implies an undirected graph; if the interviewer specifies directed edges, treat it as directed reachability.
Input
A graph adjacency list: graph[station] = [neighbor1, neighbor2, ...]
Two stations: start, end
Output
true/false: whether a path exists from start to end
Typical constraints
1 <= V <= 1e5
0 <= E <= 2e5
Sample tests (5)
Edges: A-B, B-C, query A -> C → true
Edges: A-B, C-D, query A -> D → false
Single node A, query A -> A → true
Cycle A-B, B-C, C-A, query B -> A → true
Chain A-B, B-C, C-D, D-E, query A -> E → true
Example
Input
3
A B
B C
C
A C
Output
true