Hamutaro - Hamtaro 4

Algorithm/BOJ

[Silver III/JAVA] 10451 ์ˆœ์—ด ์‚ฌ์ดํด

carsumin 2024. 11. 23. 16:31

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

 

 

  • DFS, BFS ๋‘ ๊ฐ€์ง€๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ
  • ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N)์œผ๋กœ DFS, BFS ๋™์ผํ•˜๋‹ค.

 

DFS (๊นŠ์ด์šฐ์„ ํƒ์ƒ‰) ํ’€์ด

 

  • ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด ์ˆœ์—ด์„ 1๋ถ€ํ„ฐ ์„ธ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์˜ ํฌ๊ธฐ ์„ค์ •์— ์ฃผ์˜ํ•œ๋‹ค.
  • ์ž…๋ ฅ๋ฐ›์€ ์ˆœ์—ด์„ ๋ฐฐ์—ด์— ๋„ฃ์„ ๋•Œ๋„ ArrayIndexOutOfBoundsException๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•œ๋‹ค.
  • ์ˆœ์—ด ๋ฐฐ์—ด์— ์ธ๋ฑ์Šค 1๋ถ€ํ„ฐ ๊ฐ’์„ ๋„ฃ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” str ๋ฐฐ์—ด์—์„œ๋Š” index-1์„ ํ•ด์ค˜์•ผํ•œ๋‹ค.
  • ๋…ธ๋“œ ๋ฐฉ๋ฌธ์„ ๊ธฐ๋กํ•  boolean ํƒ€์ž…์˜ visited ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์ดํด์ด ์ค‘๋ณต์œผ๋กœ ์ฒดํฌ๋˜์ง€ ์•Š๋„๋ก ํƒ์ƒ‰ํ•œ๋‹ค.

[ ํƒ์ƒ‰ ๊ณผ์ • ]

 

* n = 1

dfs(1)

visited[1] = true

nextNode = 3

 

dfs(3) 

visited[3] = true

nextNode = 7

 

dfs(7)

visited[7] = true

nextNode = 5

 

dfs(5)

visited[5] = true

nextNode = 1

-> visited[1] = true ์ด๋ฏ€๋กœ ํƒ์ƒ‰ ์ข…๋ฃŒํ•˜๊ณ  cycle์„ ์นด์šดํŠธํ•œ๋‹ค.

 

์ฒซ๋ฒˆ์งธ ํƒ์ƒ‰์ด ๋๋‚˜๋ฉด visited ๋ฐฐ์—ด์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํƒœ๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

1 2 3 4 5 6 7 8
true false true false true false true false

 

  • ์ด๋Ÿฐ์‹์œผ๋กœ dfs ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํƒ์ƒ‰์ด ํ•œ ๋ฒˆ ์™„๋ฃŒ๋˜๋ฉด ์ˆœ์—ด ์‚ฌ์ดํด์„ ์นด์šดํŠธํ•œ๋‹ค.
import java.io.*;
import java.util.*;

public class Main{
    static boolean[] visited;
    static int[] arr;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜ ์ž…๋ ฅ
        int T = Integer.parseInt(br.readLine());
        
        for(int i=0; i<T; i++){
            //์ˆœ์—ดํฌ๊ธฐ ์ž…๋ ฅ
            int N = Integer.parseInt(br.readLine());
            arr = new int[N+1];
            
            //๋…ธ๋“œ ๋ฐฉ๋ฌธ ๊ธฐ๋กํ•  ๋ฐฐ์—ด
            visited = new boolean[N+1];
            
            //์ˆœ์—ด ์ž…๋ ฅ
            String[] str = br.readLine().split(" ");           
            for(int j=1; j<=N; j++){
                arr[j] = Integer.parseInt(str[j-1]);
            } 
            
            int cycle = 0;
            
            //ํƒ์ƒ‰
            for(int n=1; n<=N; n++){
                if(!visited[n]){ //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘
                    dfs(n);
                    cycle++;
                }                
            }
            
            System.out.println(cycle);
        }
    }
    
    //DFS
    static void dfs(int node){
        //ํ˜„์žฌ ๋…ธ๋“œ ๋ฐฉ๋ฌธ
        visited[node] = true;
        int nextNode = arr[node];
        if(!visited[nextNode]){ //๋‹ค์Œ ๋…ธ๋“œ ๋ฐฉ๋ฌธ์•ˆํ–ˆ์œผ๋ฉด ํƒ์ƒ‰
            dfs(nextNode);
        }
    }
}

 

 

BFS (๋„ˆ๋น„์šฐ์„ ํƒ์ƒ‰) ํ’€์ด

 

  • ์ „์ฒด์ ์ธ ์ฝ”๋“œ๋Š” DFS์™€ ๋˜‘๊ฐ™๊ณ  ํƒ์ƒ‰ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.
  • Queue๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
  • ์ด ๋ฌธ์ œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ดค์„ ๋•Œ ๊ฐœ์ธ์ ์œผ๋กœ DFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข€ ๋” ๊ฐ„๋‹จํ•˜๋‹ค๊ณ  ๋А๊ผˆ๋‹ค.
  • Queue์—์„œ poll()์€ ํ˜„์žฌ๊ฐ’์„ ๋ฆฌํ„ด, ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

import java.io.*;
import java.util.*;

public class Main{
    static boolean[] visited;
    static int[] arr;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜ ์ž…๋ ฅ
        int T = Integer.parseInt(br.readLine());
        
        for(int i=0; i<T; i++){
            //์ˆœ์—ดํฌ๊ธฐ ์ž…๋ ฅ
            int N = Integer.parseInt(br.readLine());
            arr = new int[N+1];
            
            //๋…ธ๋“œ ๋ฐฉ๋ฌธ ๊ธฐ๋กํ•  ๋ฐฐ์—ด
            visited = new boolean[N+1];
            
            //์ˆœ์—ด ์ž…๋ ฅ
            String[] str = br.readLine().split(" ");           
            for(int j=1; j<=N; j++){
                arr[j] = Integer.parseInt(str[j-1]);
            } 
            
            int cycle = 0;
            
            //ํƒ์ƒ‰
            for(int n=1; n<=N; n++){
                if(!visited[n]){ //๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘
                    bfs(n);
                    cycle++;
                }                
            }
            
            System.out.println(cycle);
        }
    }
    
    //BFS
    static void bfs(int start){
        //Queue ์ƒ์„ฑ
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);
        visited[start] = true;
        
        while(!queue.isEmpty()){
            int current = queue.poll(); //ํ˜„์žฌ ๋…ธ๋“œ
            int nextNode = arr[current]; //์ˆœ์—ด์— ๋”ฐ๋ฅธ ์ด๋™
            
            if(!visited[nextNode]){
                visited[nextNode] = true;
                queue.add(nextNode);
            }
        }
    }
}