Skip to main content

๐Ÿ“ ๋ฐฑ์ค€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๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ

image.png


2. ์ ‘๊ทผ๋ฐฉ๋ฒ•

๊ณตํ†ต์‚ฌํ•ญ

  1. ๊ทธ๋ž˜ํ”„
    โ†’ ์ธ์ ‘๋ฆฌ์ŠคํŠธ(ArrayList<Integer>[])์— ์ €์žฅ
    โ†’ DFS/BFS ๊ตฌํ˜„ ์ „์— ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ ์™œ? ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธ
  2. ๋ฐฉ๋ฌธ ์ฒดํฌ ๋ฐฐ์—ด (boolean[] visited)
  3. 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);
                }
            }
        }
    }
}

image.png

์ž๋ฐ” ์ œ๋„ค๋ฆญ์ด๋ž‘ ๋ฐฐ์—ด์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๋•Œ ์ž์ฃผ ๋‚˜์˜ค๋Š” ๊ฒฝ๊ณ ๋ผ๊ณ  ํ•œ๋‹ค.ย 

  • ์ž๋ฐ”์—์„œ๋Š”ย ์ œ๋„ค๋ฆญ ๋ฐฐ์—ด ์ƒ์„ฑ์€ ์ง์ ‘ ๋ถˆ๊ฐ€ํ•˜๋‹ค. โ†’ย ์ฆ‰, 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")ย โ†’ ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ  ์ œ๊ฑฐ