-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathExists.java
37 lines (33 loc) · 1.13 KB
/
PathExists.java
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
32
33
34
35
36
37
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PathExists {
public static void main(String[] args) {
PathExists tester = new PathExists();
int N = 416;
List<List<Integer>> edges = Collections.emptyList();
tester.solve(N,edges);
}
public int solve(int N, List<List<Integer>> edges) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i=1; i<=N; i++) {
graph.put(i, new ArrayList<Integer>());
}
for (List<Integer> edge:edges) {
graph.get(edge.get(0)).add(edge.get(1));
}
boolean[] visited = new boolean[N+1];
return dfs(graph, visited, 1, N) ;
}
int dfs(Map<Integer, List<Integer>> graph, boolean[] visited, Integer source, Integer destination) {
if (visited[source]) return 0;
if (source == destination) return 1;
visited[source] = true;
for (Integer neighbor:graph.get(source)) {
if (dfs(graph, visited, neighbor, destination) == 1) return 1;
}
return 0;
}
}