-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1971.cpp
More file actions
21 lines (21 loc) · 802 Bytes
/
1971.cpp
File metadata and controls
21 lines (21 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
bool dfs(vector<bool>& visited, vector<vector<int>>& adjacency, int current, int destination) {
visited[current] = true;
if (current == destination) return true;
for (auto& neighbor : adjacency[current]) {
if (visited[neighbor]) continue;
if (dfs(visited, adjacency, neighbor, destination)) return true;
}
return false;
}
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
vector<bool> visited(n, false);
vector<vector<int>> adjacency(n);
for (auto& edge : edges) {
adjacency[edge[0]].push_back(edge[1]);
adjacency[edge[1]].push_back(edge[0]);
}
return dfs(visited, adjacency, source, destination);
}
};