-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon1260.java
94 lines (75 loc) · 2.51 KB
/
Baekjoon1260.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package algo.Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* https://www.acmicpc.net/problem/1260
* 백준 DFS와 BFS
*/
public class Baekjoon1260 {
private static Integer N, M, V;
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int[][] map;
private static boolean[] visited;
private static List<Integer> dfsList = new ArrayList<>();
private static List<Integer> bfsList = new ArrayList<>();
private static boolean dfsFlag;
public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.valueOf(st.nextToken());
M = Integer.valueOf(st.nextToken());
V = Integer.valueOf(st.nextToken());
map = new int[N + 1][N + 1];
visited = new boolean[N + 1];
for (int i = 1; i <= M; i++) {
st = new StringTokenizer(br.readLine());
int first = Integer.parseInt(st.nextToken());
int second = Integer.parseInt(st.nextToken());
map[first][second] = 1;
map[second][first] = 1;
}
visited[V] = true;
dfs(V);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dfsList.size(); i++) {
sb.append(dfsList.get(i))
.append(" ");
}
System.out.println(sb.toString().trim());
visited = new boolean[N+1];
bfs();
}
private static void bfs() {
Queue<Integer> queue = new LinkedList();
visited[V] = true;
queue.add(V);
while(!queue.isEmpty()){
int current = queue.poll();
bfsList.add(current);
for (int i = 1; i < N+1; i++) {
if(map[current][i] == 1 && !visited[i]){
visited[i] = true;
queue.add(i);
}
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bfsList.size(); i++) {
sb.append(bfsList.get(i))
.append(" ");
}
System.out.println(sb.toString().trim());
}
private static void dfs(Integer current) {
dfsList.add(current);
int count = 0;
for (int i = 1; i < N+1; i++) {
if(map[current][i] == 1 && !visited[i]){
count++;
visited[i] = true;
dfs(i);
}
}
}
}