-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path경로탐색_인접리스트.java
50 lines (39 loc) · 1.14 KB
/
경로탐색_인접리스트.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
38
39
40
41
42
43
44
45
46
47
48
49
50
package 인프런.Section07;
import java.util.ArrayList;
import java.util.Scanner;
public class 경로탐색_인접리스트 {
static int n, m, answer = 0;
static ArrayList<ArrayList<Integer>> graph;
static int[] check;
public void DFS(int v) {
if(v==n) answer++;
else {
for(int nv : graph.get(v)) {
if(check[nv] == 0) {
check[nv] = 1;
DFS(nv);
check[nv] = 0;
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
경로탐색_인접리스트 t = new 경로탐색_인접리스트();
n = scanner.nextInt();
m = scanner.nextInt();
graph = new ArrayList<>();
for(int i = 0; i <= n; i++) {
graph.add(new ArrayList<>());
}
check = new int[n+1];
for(int i = 0; i < m; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
graph.get(a).add(b);
}
check[1] = 1;
t.DFS(1);
System.out.print(answer);
}
}