-
Notifications
You must be signed in to change notification settings - Fork 96
/
[14]_1.java
69 lines (59 loc) · 1.39 KB
/
[14]_1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static int n, m, start;
public static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
public static boolean[] d = new boolean[1001];
// 깊이 우선 탐색(DFS)
public static void dfs(int x) {
if (d[x]) return;
d[x] = true;
System.out.print(x + " ");
for (int i = 0; i < adj.get(x).size(); i++) {
int y = adj.get(x).get(i);
dfs(y);
}
}
// 너비 우선 탐색(BFS)
public static void bfs(int x) {
Queue<Integer> q = new LinkedList<Integer>();
q.offer(x);
d[x] = true;
while (!q.isEmpty()) {
int now = q.poll();
System.out.print(now + " ");
for (int i = 0; i < adj.get(now).size(); i++) {
int y = adj.get(now).get(i);
if (!d[y]) {
q.offer(y);
d[y] = true;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
start = sc.nextInt();
for (int i = 0; i <= n; i++) {
adj.add(new ArrayList<Integer>());
}
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
adj.get(x).add(y);
adj.get(y).add(x);
}
for(int i = 1; i <= n; i++) {
Collections.sort(adj.get(i));
}
dfs(start);
System.out.println();
d = new boolean[1001];
bfs(start);
}
}