Skip to content

Commit 4f8afbf

Browse files
committed
1260 DFS와 BFS
1 parent c84942e commit 4f8afbf

File tree

1 file changed

+51
-66
lines changed

1 file changed

+51
-66
lines changed
Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,77 @@
11
package dfs.silver.silver_2;
2-
import java.io.BufferedReader;
3-
import java.io.IOException;
4-
import java.io.InputStreamReader;
2+
3+
import java.io.*;
54
import java.util.LinkedList;
65
import java.util.Queue;
76
import java.util.StringTokenizer;
8-
public class _1260 {
9-
static StringBuilder sb = new StringBuilder();
10-
static boolean[] check;
11-
static int[][] arr;
12-
static int node, N, M, V;
137

14-
static Queue<Integer> q = new LinkedList<>();
8+
public class _1260 {
159

16-
public static void dfs(int V) {
17-
check[V] = true;
18-
sb.append(V + " "); // 첫번째 값이 들어갔으므로 sb에 저장
10+
private static int n;
11+
private static int[][] graph;
12+
private static boolean[] visited;
13+
private static StringBuilder sb;
1914

20-
for(int i = 1; i <= N; i++){
21-
if(arr[V][i] == 1 && !check[i]){ // 탐색하지 않은 수들 확인
22-
dfs(i);
23-
}
24-
}
25-
}
26-
public static void bfs(int V) {
27-
q.offer(V);
28-
check[V] = true;
29-
30-
while(!q.isEmpty()){
31-
V = q.poll(); // Queue의 맨 앞의 값을 빼주고
32-
sb.append(V + " "); // sb에 저장함
33-
34-
for(int i = 1; i <= N; i++){
35-
if(arr[V][i] == 1 && !check[i]){ // 탐색하지 않은 수들 확인
36-
q.offer(i); // Queue 성질 이용 없으면 넣고 check해준다
37-
check[i] = true;
38-
}
39-
}
40-
}
41-
}
42-
public static void main(String[] args) throws IOException{
15+
public static void main(String[] args) throws IOException {
4316
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
4418
StringTokenizer st = new StringTokenizer(br.readLine());
19+
sb = new StringBuilder();
4520

46-
N = Integer.parseInt(st.nextToken()); // 정점 개수
47-
M = Integer.parseInt(st.nextToken()); // 간선 개수
48-
V = Integer.parseInt(st.nextToken()); // 시작할 정점 번호
21+
n = Integer.parseInt(st.nextToken());
22+
int m = Integer.parseInt(st.nextToken());
23+
int v = Integer.parseInt(st.nextToken());
4924

50-
arr = new int[N + 1][N + 1];
51-
check = new boolean[N + 1]; // 탐색한 내용을 다시 탐색하지 않도록 설정
25+
graph = new int[n + 1][n + 1];
5226

53-
for(int i = 0; i < M; i++){
27+
for (int i = 0; i < m; i++) {
5428
st = new StringTokenizer(br.readLine());
29+
int x = Integer.parseInt(st.nextToken());
30+
int y = Integer.parseInt(st.nextToken());
31+
graph[x][y] = graph[y][x] = 1;
32+
}
5533

56-
int a = Integer.parseInt(st.nextToken());
57-
int b = Integer.parseInt(st.nextToken());
34+
visited = new boolean[n + 1];
35+
dfs(v);
5836

59-
arr[a][b] = arr[b][a] = 1; // a -> b && b -> 로 갈 수 있다는 코드
60-
}
61-
dfs(V);
62-
sb.append('\n');
37+
sb.append("\n");
6338

64-
check = new boolean[N + 1]; // bfs에서 check해준 값을 가져옴
65-
bfs(V);
39+
visited = new boolean[n + 1];
40+
bfs(v);
6641

67-
System.out.println(sb);
42+
bw.write(sb.toString());
43+
bw.flush();
44+
bw.close();
6845
}
69-
}
70-
/*
71-
Stack<Integer> stack = new Stack<>();
72-
visited = new boolean[N + 1];
7346

74-
stack.push(V);
47+
private static void bfs(int v) {
48+
Queue<Integer> queue = new LinkedList<>();
49+
queue.add(v);
50+
visited[v] = true;
7551

76-
while (!stack.isEmpty()){
77-
int tmp = stack.pop();
52+
while (!queue.isEmpty()) {
53+
int node = queue.poll();
54+
sb.append(node).append(" ");
7855

79-
if(visited[tmp]){
80-
continue;
56+
for (int i = 1; i <= n; i++) {
57+
if (!visited[i] && graph[node][i] == 1) {
58+
visited[i] = true;
59+
queue.offer(i);
60+
}
8161
}
82-
visited[tmp] = true;
83-
sb.append(tmp + " ");
62+
}
63+
}
8464

85-
for(int i = 1; i <= N; i++){
86-
if(!visited[i] && arr[tmp][i] == 1){
87-
stack.push(i);
88-
}
65+
private static void dfs(int v) {
66+
visited[v] = true;
67+
sb.append(v).append(" ");
68+
69+
for (int i = 1; i <= n; i++) {
70+
if (!visited[i] && graph[v][i] == 1) {
71+
visited[i] = true;
72+
dfs(i);
8973
}
9074
}
9175
}
92-
*/
76+
77+
}

0 commit comments

Comments
 (0)