๐ ๋ฐฑ์ค1260 DFS์ BFS
https://www.acmicpc.net/problem/1260
1. ๋ฌธ์ ์์ฝ
- ๊ทธ๋ํ๋ฅผ DFS๋ก ํ์ํ ๊ฒฐ๊ณผ์ BFS๋ก ํ์ํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ๊ธฐ
- ๋จ, ๋ฐฉ๋ฌธํ ์ ์๋ ์ ์ ์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ์๋ ์ ์ ๋ฒํธ๊ฐ ์์ ๊ฒ์ ๋จผ์ ๋ฐฉ๋ฌธํ๊ณ ,
- ๋ ์ด์ ๋ฐฉ๋ฌธํ ์ ์๋ ์ ์ด ์๋ ๊ฒฝ์ฐ ์ข ๋ฃํ๋ค.ย ์ ์ ๋ฒํธ๋ 1๋ฒ๋ถํฐ N๋ฒ๊น์ง์ด๋ค.
- ์
๋ ฅ :
- ์ฒซ์งธ์ค
- ์ ์ ์ ๊ฐ์ N(1 โค N โค 1,000),
- ๊ฐ์ ์ ๊ฐ์ M(1 โค M โค 10,000),
- ํ์์ ์์ํ ์ ์ ์ ๋ฒํธ V
- ๋ค์ M๊ฐ์ ์ค์๋ ๊ฐ์ ์ด ์ฐ๊ฒฐํ๋ ๋ ์ ์ ์ ๋ฒํธ
- ์ฒซ์งธ์ค
2. ์ ๊ทผ๋ฐฉ๋ฒ
๊ณตํต์ฌํญ
- ๊ทธ๋ํ
โ ์ธ์ ๋ฆฌ์คํธ(ArrayList<Integer>[])์ ์ ์ฅ
โ DFS/BFS ๊ตฌํ ์ ์ ์ธ์ ๋ฆฌ์คํธ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๊ธฐ ์? ์ ์ ๋ฒํธ๊ฐ ์์ ์์๋๋ก ๋ฐฉ๋ฌธ - ๋ฐฉ๋ฌธ ์ฒดํฌ ๋ฐฐ์ด (boolean[] visited)
- StringBuilder๋ก ๋ฐฉ๋ฌธํ ์ ์ ์์๊ธฐ๋กํ๊ธฐ โ ์ถ๋ ฅ
DFS(๊น์ด ์ฐ์ ํ์)
- ์คํ/์ฌ๊ท๋ก ๊ตฌํ๊ฐ๋ฅํ๋ฐ ์ฌ๊ธฐ์ ์ฌ๊ท๋ก ํ ๋ฒ ๊ตฌํํด๋ณด๋ ค๊ณ ํ๋ค
- โํ์ฌ ์ ์ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌํ ์ธ์ ํ ์ ์ ์ํํ๋ฉฐ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ ๋ํด ์ฌ๊ท ํธ์ถ
- ์ฌ๊ทํธ์ถ ์์ = ๋ฐฉ๋ฌธ ์์
BFS(๋๋น ์ฐ์ ํ์)
- ํ๋ฅผ ์ฌ์ฉํด์ ๊ตฌํ
- ํ์์ ์ ์ ์ ๊บผ๋ธ ๋ค, ๊ทธ ์ ์ ๊ณผ ์ฐ๊ฒฐ๋ ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ ์์ ๋ฒํธ์์ผ๋ก ํ์ ๋ฃ๋๋ค
- ์ด๋ ๊ฒ ํด์ผ BFS์์๋ ์์ ๋ฒํธ๋ถํฐ ๋ฐฉ๋ฌธ์ ํ๊ฒ ๋๋ค.
3. ์ค๋ต์ฝ๋
/*
Welcome to JDoodle!
You can execute code here in 88 languages. Right now youโre in the Java IDE.
1. Click the orange Execute button โถ to execute the sample code below and see how it works.
2. Want help writing or debugging code? Type a query into JDroid on the right hand side ---------------->
3.Try the menu buttons on the left. Save your file, share code with friends and open saved projects.
Want to change languages? Try the search bar up the top.
*/
import java.util.*;
public class Main {
static ArrayList<Integer>[] graph; // ์ธ์ ๋ฆฌ์คํธ
static boolean[] visited; // ๋ฐฉ๋ฌธ ์ฒดํฌ
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // ์ ์ ๊ฐ์
int M = sc.nextInt(); // ๊ฐ์ ๊ฐ์
int V = sc.nextInt(); // ์์ ์ ์
// 0๋ฒ ์ธ๋ฑ์ค ์์ฐ๊ณ 1~N๊น์ง ์ฐ๋ ค๊ณ ์ด๋ ๊ฒ ์ ์ธํจ
graph = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
graph[i] = new ArrayList<>();
}
// ๊ฐ์ ์
๋ ฅํ๊ธฐ
for (int i = 0; i < M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
// ์๋ฐฉํฅ์ด๋ผ์ a๋ b์ฐ๊ฒฐ, b์๋ a์ฐ๊ฒฐ
graph[a].add(b);
graph[b].add(a);
}
// ์ค๋ฆ์ฐจ์ ์ ์ ๋ฒํธ ์์ ๊ฒ๋ถํฐ
for (int i = 1; i <= N; i++) {
Collections.sort(graph[i]);
}
/*
graph[0] = null
graph[1] = [2, 3, 4]
graph[2] = [1, 4]
graph[3] = [1, 4]
graph[4] = [1, 2, 3]
*/
// DFS
visited = new boolean[N + 1];
dfs(V);
sb.append("\n");
// BFS
visited = new boolean[N + 1];
bfs(V);
System.out.println(sb.toString());
}
// DFS (์ฌ๊ท)
static void dfs(int node) { // node๋ ํ์ฌ ๋ฐฉ๋ฌธํ๊ณ ์๋ ๋
ธ๋ ๋ฒํธ
// node = V์์ ์์
// ๋ฐฉ๋ฌธ ๊ธฐ๋ก
visited[node] = true;
sb.append(node).append(" ");
// ํ์ฌ ์ ์ ์ ์ธ์ ๋
ธ๋๋ค์ ํ๋์ฉ ํ์ธ
// ์์ง ์ ๊ฐ๋ณธ ๊ณณ์ด๋ฉด DFS ์ฌ๊ท๋ก ๋ค์ด๊ฐ๋ค
for (int next : graph[node]) { // ํ์ฌ ์ ์ node์ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋ ๋ฆฌ์คํธ
// ๋ง์ฝ์ node = 1์ด๊ณ grpah[1] = [2, 3, 4]๋ฉด next๋ 2, 3, 4
// ํ์ฌ ๋
ธ๋์์ ๊ฐ ์ ์๋ ์ธ์ ๋
ธ๋๋ค์ ํ๋์ฉ ํ์ธํ๊ธฐ
if (!visited[next]) {
// ๋ฐฉ๋ฌธํ์ง ์์ ์ธ์ ๋
ธ๋ next๋ก ์ฌ๊ทํธ์ถํด์ ์ด ๋
ธ๋๋ถํฐ DFS ์์
dfs(next);
}
}
}
// BFS (ํ)
// ๋จผ์ ๋ค์ด์จ ๋
ธ๋๋ถํฐ ์ฒ๋ฆฌํ๊ณ LinkedList๋ฅผ Queue ์ธํฐํ์ด์ค๋ก ๊ตฌํ
static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
q.add(start); // ์์ ๋
ธ๋ ํ์ ๋ฃ๊ณ
visited[start] = true; //๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
while (!q.isEmpty()) {
int node = q.poll(); // ๋จผ์ ๋ค์ด์จ ๋
ธ๋ ๊บผ๋ด๊ธฐ
sb.append(node).append(" ");
// ์ธ์ ๋
ธ๋ ํ์
for (int next : graph[node]) {
if (!visited[next]) {
visited[next] = true;
q.add(next);
}
}
}
}
}
์๋ฐ ์ ๋ค๋ฆญ์ด๋ ๋ฐฐ์ด์ ํจ๊ป ์ฌ์ฉํ ๋ ์์ฃผ ๋์ค๋ ๊ฒฝ๊ณ ๋ผ๊ณ ํ๋ค.ย
- ์๋ฐ์์๋ย ์ ๋ค๋ฆญ ๋ฐฐ์ด ์์ฑ์ ์ง์ ๋ถ๊ฐํ๋ค. โย ์ฆ, new ArrayList[N+1]์ ๋ถ๊ฐ๋ฅ ํ๊ธฐ ๋๋ฌธ์
- ์ ๋ค๋ฆญ์ ๋นผ๊ณ ๊ทธ๋ฅ Raw ํ์ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ย ๊ฐ ์ธ๋ฑ์ค๋ฅผ ArrayList<Integer>๋ก ์ด๊ธฐํํ๋ค.
- ์ด๋ ๊ฒ ํ๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ํ์ ์์ ์ฑ์ด ์์ ํ ๋ณด์ฅ๋์ง ์๋๋ค๊ณ ์ปดํ์ผ ์๋ฌ๋ฅผ ๋์ด๋ค.ย
- ๊ฒฝ๊ณ ๋ ๋จ์ง๋ง, graph[i]์ ํ์ ์ด ์ผ์นํ๋ ๋ฐ์ดํฐ๋ง ๋ค์ด์ค๋ฉด ์ค์ ๋ก ๋ฌธ์ ์๋ค๊ณ ํ๋ค.ย
๊ทธ๋ฅ @SuppressWarnings("unchecked")๋ก ๊ฒฝ๊ณ ๋ง ์ ๊ฑฐํ๊ณ ์ฌ์ฉํด๋ ๋๊ณ ArrayList๋ฅผ (List<Integer>[])๋ก ๊ฐ์ธ์ ํ์ ์บ์คํ ํ๋ฉด ๋๋ค.
4. ์ ๋ต์ฝ๋
import java.util.*;
public class Main {
/* static ArrayList<Integer>[] graph; */
static List<Integer>[] graph; // ์ธ์ ๋ฆฌ์คํธ
static boolean[] visited; // ๋ฐฉ๋ฌธ ์ฒดํฌ
static StringBuilder sb = new StringBuilder();
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt(); // ์ ์ ๊ฐ์
int M = sc.nextInt(); // ๊ฐ์ ๊ฐ์
int V = sc.nextInt(); // ์์ ์ ์
// 0๋ฒ ์ธ๋ฑ์ค ์์ฐ๊ณ 1~N๊น์ง ์ฐ๋ ค๊ณ ์ด๋ ๊ฒ ์ ์ธํจ
/*
graph = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
graph[i] = new ArrayList<>();
}
*/
graph = (List<Integer>[]) new List[N + 1];
for (int i = 1; i <= N; i++) {
graph[i] = new ArrayList<>();
}
// ๊ฐ์ ์
๋ ฅํ๊ธฐ
for (int i = 0; i < M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
// ์๋ฐฉํฅ์ด๋ผ์ a๋ b์ฐ๊ฒฐ, b์๋ a์ฐ๊ฒฐ
graph[a].add(b);
graph[b].add(a);
}
// ์ค๋ฆ์ฐจ์ ์ ์ ๋ฒํธ ์์ ๊ฒ๋ถํฐ
for (int i = 1; i <= N; i++) {
Collections.sort(graph[i]);
}
/*
graph[0] = null
graph[1] = [2, 3, 4]
graph[2] = [1, 4]
graph[3] = [1, 4]
graph[4] = [1, 2, 3]
*/
// DFS
visited = new boolean[N + 1];
dfs(V);
sb.append("\n");
// BFS
visited = new boolean[N + 1];
bfs(V);
System.out.println(sb.toString());
}
// DFS (์ฌ๊ท)
static void dfs(int node) { // node๋ ํ์ฌ ๋ฐฉ๋ฌธํ๊ณ ์๋ ๋
ธ๋ ๋ฒํธ
// node = V์์ ์์
// ๋ฐฉ๋ฌธ ๊ธฐ๋ก
visited[node] = true;
sb.append(node).append(" ");
// ํ์ฌ ์ ์ ์ ์ธ์ ๋
ธ๋๋ค์ ํ๋์ฉ ํ์ธ
// ์์ง ์ ๊ฐ๋ณธ ๊ณณ์ด๋ฉด DFS ์ฌ๊ท๋ก ๋ค์ด๊ฐ๋ค
for (int next : graph[node]) { // ํ์ฌ ์ ์ node์ ์ฐ๊ฒฐ๋ ๋ชจ๋ ๋
ธ๋ ๋ฆฌ์คํธ
// ๋ง์ฝ์ node = 1์ด๊ณ grpah[1] = [2, 3, 4]๋ฉด next๋ 2, 3, 4
// ํ์ฌ ๋
ธ๋์์ ๊ฐ ์ ์๋ ์ธ์ ๋
ธ๋๋ค์ ํ๋์ฉ ํ์ธํ๊ธฐ
if (!visited[next]) {
// ๋ฐฉ๋ฌธํ์ง ์์ ์ธ์ ๋
ธ๋ next๋ก ์ฌ๊ทํธ์ถํด์ ์ด ๋
ธ๋๋ถํฐ DFS ์์
dfs(next);
}
}
}
// BFS (ํ)
// ๋จผ์ ๋ค์ด์จ ๋
ธ๋๋ถํฐ ์ฒ๋ฆฌํ๊ณ LinkedList๋ฅผ Queue ์ธํฐํ์ด์ค๋ก ๊ตฌํ
static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
q.add(start); // ์์ ๋
ธ๋ ํ์ ๋ฃ๊ณ
visited[start] = true; //๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
while (!q.isEmpty()) {
int node = q.poll(); // ๋จผ์ ๋ค์ด์จ ๋
ธ๋ ๊บผ๋ด๊ธฐ
sb.append(node).append(" ");
// ์ธ์ ๋
ธ๋ ํ์
for (int next : graph[node]) {
if (!visited[next]) {
visited[next] = true;
q.add(next);
}
}
}
}
}
List<Integer>[] graph โ ArrayList
ย ๋์ ยList
ย ์ธํฐํ์ด์ค ์ฌ์ฉgraph = (List<Integer>[]) new List[N + 1];
ย โ ์ ๋ค๋ฆญ ๋ฐฐ์ด ์์ฑ ์ ํ์ ์บ์คํ@SuppressWarnings("unchecked")
ย โ ์ปดํ์ผ๋ฌ ๊ฒฝ๊ณ ์ ๊ฑฐ