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