Skip to content

Commit 2f72153

Browse files
authored
Create 1059.All-Paths-from-Source-Lead-to-Destination.cpp
1 parent c8e2b15 commit 2f72153

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
int visited[10001];
3+
vector<vector<int>>nextNodes;
4+
public:
5+
bool leadsToDestination(int n, vector<vector<int>>& edges, int source, int destination)
6+
{
7+
nextNodes.resize(n+1);
8+
for (auto edge: edges)
9+
nextNodes[edge[0]].push_back(edge[1]);
10+
11+
return dfs(source, destination);
12+
}
13+
14+
bool dfs(int cur, int dest)
15+
{
16+
visited[cur] = 2;
17+
if (nextNodes[cur].size() == 0)
18+
{
19+
if (cur!=dest)
20+
return false;
21+
else
22+
{
23+
visited[cur] = 1;
24+
return true;
25+
}
26+
}
27+
28+
for (auto next: nextNodes[cur])
29+
{
30+
if (visited[next]==1) continue;
31+
if (visited[next]==2) return false;
32+
if (dfs(next, dest)==false)
33+
return false;
34+
}
35+
visited[cur] = 1;
36+
return true;
37+
}
38+
};

0 commit comments

Comments
 (0)