-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_1260_DFS와BFS.java
More file actions
64 lines (54 loc) · 1.55 KB
/
Copy pathMain_1260_DFS와BFS.java
File metadata and controls
64 lines (54 loc) · 1.55 KB
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
package algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main_1260_DFS와BFS {
static int [][] Gragh;
static boolean [] check;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine()," ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
Gragh = new int [N+1][N+1];
for (int i = 1; i <= M; i++) {
st = new StringTokenizer(in.readLine()," ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
Gragh[x][y] = Gragh[y][x] = 1;
}
check = new boolean [N+1];
dfs(V,N);
System.out.println("");
check = new boolean [N+1];
bfs(V, N);
}
public static void dfs(int V, int N) {
check[V] = true;
System.out.print(V + " ");
for (int i = 1; i < N+1; i++) {
if(Gragh[V][i] == 1 && !check[i]) {
dfs(i, N);
}
}
}
public static void bfs(int V, int N) {
Queue<Integer> que = new LinkedList<Integer>();
que.add(V);
check[V] = true;
while (!que.isEmpty()) {
int temp = que.poll();
System.out.print(temp + " ");
for (int i = 1; i < N+1; i++) {
if(Gragh[temp][i] == 1 && !check[i]) {
que.add(i);
check[i] = true;
}
}
}
}
}