-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path797.cpp
More file actions
31 lines (31 loc) · 787 Bytes
/
797.cpp
File metadata and controls
31 lines (31 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
vector<vector<int>> res;
vector<bool> visited;
int n;
void dfs(int curr, vector<vector<int>>& graph, vector<int>& tmp){
if(curr== n-1){
tmp.push_back(curr);
res.push_back(tmp);
tmp.pop_back();
return;
}
tmp.push_back(curr);
for(auto v: graph[curr]){
if(!visited[v]){
visited[curr] = 1;
dfs(v,graph,tmp);
visited[curr] = 0;
}
}
tmp.pop_back();
return;
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
n = graph.size();
visited.resize(n,false);
vector<int> tmp;
dfs(0,graph,tmp);
return res;
}
};