Hamutaro - Hamtaro 4

Algorithm/BOJ

[Silver II/JAVA] 1260 DFS์™€ BFS

carsumin 2024. 12. 10. 00:29

https://www.acmicpc.net/problem/1260

 

 

 

  • ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์ •์ ์˜ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•œ๋‹ค. (์ž…๋ ฅ์˜ˆ์‹œ)
    • 1 2 ์ž…๋ ฅ -> graph = [ [ ], [2], [1], [ ] , [ ] ]
    • 1 3 ์ž…๋ ฅ -> graph = [ [ ], [2, 3], [1], [1] , [ ] ]
    • 1 4 ์ž…๋ ฅ - > graph = [ [ ], [2, 3, 4], [1], [1] , [1] ]
    • 2 4 ์ž…๋ ฅ - > graph = [ [ ], [2, 3, 4], [1, 4], [1] , [1, 2] ]
    • 3 4 ์ž…๋ ฅ -> graph = [ [ ], [2, 3, 4], [1, 4], [1, 4] , [1, 2, 3] ]
  • ์•ž์—์„œ ํ’€์—ˆ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—… 1,2์™€ ์œ ์‚ฌํ•จ.

 

 

ํ’€์ด
import java.io.*;
import java.util.*;

public class Main{
    static boolean[] visited;
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static ArrayList<Integer> dfsList = new ArrayList<>();
    static ArrayList<Integer> bfsList = new ArrayList<>();
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String[] str = br.readLine().split(" ");
        int N = Integer.parseInt(str[0]);
        int M = Integer.parseInt(str[1]);
        int V = Integer.parseInt(str[2]);
        
        //graph ์ดˆ๊ธฐํ™”
        for(int i=0; i<=N; i++){
            graph.add(new ArrayList<>());
        }
        
        //graph์— ๊ฐ„์„  ์ž…๋ ฅ
        for(int i=0; i<M; i++){
            str = br.readLine().split(" ");
            int u = Integer.parseInt(str[0]);
            int v = Integer.parseInt(str[1]);
            
            //์–‘๋ฐฉํ–ฅ
            graph.get(u).add(v);
            graph.get(v).add(u);
        }
        
        //์˜ค๋ฆ„์ฐจ์ˆœ ๋ฐฉ๋ฌธ
        for(int i=1; i<=N; i++){
            Collections.sort(graph.get(i));
        }
        
        //DFS
        visited = new boolean[N+1];
        dfs(V);
        
        //BFS
        visited = new boolean[N+1];
        bfs(V);
        
        //์ถœ๋ ฅ
        for(int node : dfsList){
            System.out.print(node + " ");
        }
        System.out.println();
        for(int node : bfsList){
            System.out.print(node + " ");
        }
        
    }
    
    //DFS ํƒ์ƒ‰
    private static void dfs(int node){
        visited[node] = true;
        dfsList.add(node);
        
        for(int nextNode : graph.get(node)){
            if(!visited[nextNode]){
                dfs(nextNode);
            }
        }
    }
    
    //BFS ํƒ์ƒ‰
    private static void bfs(int start){
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        visited[start] = true;
        
        while(!queue.isEmpty()){
            int current = queue.poll();
            bfsList.add(current);
            
            for(int nextNode : graph.get(current)){
                if(!visited[nextNode]){
                    visited[nextNode] = true;
                    queue.add(nextNode);
                }
            }
        }
    }
}