DFS solution CPP


#1

void findPath(vector<vector > &graph,vector &visited,int &ans, int i,int A){

if(ans)return;
if(i == A){ans = 1;return;}
if(visited[i]==true)return;
visited[i]=true;
for(auto j : graph[i]){
    if(!visited[j])findPath(graph,visited,ans,j,A);
}
return;

}

int Solution::solve(int A, vector<vector > &B) {

vector<bool> visited(A+1,false);
vector<vector<int> > graph(A+1);
for(int i =0;i<B.size();i++){
    graph[B[i][0]].push_back(B[i][1]);
}
int ans = 0;
findPath(graph,visited,ans,1,A);
return ans;

}