๐Ÿฅ‡ [Baekjoon / Java] 2252. ์ค„ ์„ธ์šฐ๊ธฐ

์ดํ•˜์–€ยท2024๋…„ 6์›” 7์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
19/33

๋ฌธ์ œ ์„ค๋ช…


๐Ÿ’ก Info

๋‚ด์šฉ

N๋ช…์˜ ํ•™์ƒ๋“ค์„ ํ‚ค ์ˆœ์„œ๋Œ€๋กœ ์ค„์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ์ง์ ‘ ์žฌ์„œ ์ •๋ ฌํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒ ์ง€๋งŒ, ๋งˆ๋•…ํ•œ ๋ฐฉ๋ฒ•์ด ์—†์–ด์„œ ๋‘ ํ•™์ƒ์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๊ทธ๋‚˜๋งˆ๋„ ๋ชจ๋“  ํ•™์ƒ๋“ค์„ ๋‹ค ๋น„๊ตํ•ด ๋ณธ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋งŒ์„ ๋น„๊ตํ•ด ๋ณด์•˜๋‹ค.

์ผ๋ถ€ ํ•™์ƒ๋“ค์˜ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ค„์„ ์„ธ์šฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— N(1 โ‰ค N โ‰ค 32,000), M(1 โ‰ค M โ‰ค 100,000)์ด ์ฃผ์–ด์ง„๋‹ค.
  • M์€ ํ‚ค๋ฅผ ๋น„๊ตํ•œ ํšŸ์ˆ˜์ด๋‹ค.
  • ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ํ‚ค๋ฅผ ๋น„๊ตํ•œ ๋‘ ํ•™์ƒ์˜ ๋ฒˆํ˜ธ A, B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” ํ•™์ƒ A๊ฐ€ ํ•™์ƒ B์˜ ์•ž์— ์„œ์•ผ ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
  • ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ์ด๋‹ค.
    3 2
    1 3
    2 3
    4 2
    4 2
    3 1

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ํ•™์ƒ๋“ค์„ ์•ž์—์„œ๋ถ€ํ„ฐ ์ค„์„ ์„ธ์šด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹ต์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€์ธ ๊ฒฝ์šฐ์—๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.
    1 2 3
    4 2 3 1


๋ฌธ์ œ ์ดํ•ด


  • N : ํ•™์ƒ ์ˆ˜
  • M : ํ‚ค ๋น„๊ต ํšŸ์ˆ˜ = ์ฆ‰, ๊ตํ™˜ ํšŸ์ˆ˜
  • DFS ์ง„ํ–‰(SWEA 1244๋ฒˆ ์ตœ๋Œ€์ƒ๊ธˆ๊ณผ ๋น„์Šท)


์•Œ๊ณ ๋ฆฌ์ฆ˜


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 14๋ถ„

  • ์ž…๋ ฅ
    • ํ•™์ƒ ์ˆ˜ N ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋น„๊ต ํšŸ์ˆ˜ M ์ž…๋ ฅ๋ฐ›๊ธฐ
    • 2์ฐจ์› ๋ฐฐ์—ด arr ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ
    • dfs ๊ณ„์‚ฐํ•˜๊ธฐ
  • ์ถœ๋ ฅ
    • ๊ฒฐ๊ณผ result ์ถœ๋ ฅํ•˜๊ธฐ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ
    • DFS ์ง„ํ–‰
      • ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋Š” true๋กœ ๋ณ€๊ฒฝ
      • ๋…ธ๋“œ ์ถœ๋ ฅ
      • ๋งŒ์•ฝ ๋…ธ๋“œ๊ฐ€ arr.length์— ๋‹ค๋‹ค๋ž๋‹ค๋ฉด โ†’ return;
      • for๋ฌธ
        • if(arr[V][i] == 1 && !visited[i]) dfs(i);
import java.util.*;

public class Main {
    static int N;
    static int M;
    static int[][] arr;
    static boolean[] visited;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        M = sc.nextInt();

        sc.nextLine();

        arr = new int[N + 1][N + 1];
        for (int i = 0; i < M; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            arr[from][to] = 1;
            arr[to][from] = 1;
        }

        visited = new boolean[N + 1];

        dfs(1); // ์ •์ ์ด 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ

        System.out.println();
    }

    public static void dfs(int V) {
        visited[V] = true;

        System.out.print(V + " ");

        for (int i = 1; i <= N; i++) {
            if (arr[V][i] == 1 && !visited[i])
                dfs(i);
        }
    }
}


์˜ค๋‹ต ์ฒดํฌ


  • DFS๋กœ ์ง„ํ–‰ํ•˜๋‹ˆ, ๊ฒฐ๊ณผ ๊ฐ’์ด 1 2 3์ด ์•„๋‹Œ 1 3 2๊ฐ€ ๋‚˜์˜ด!!
    • BFS๋กœ ๋ณ€๊ฒฝ
//before
    public static void dfs(int V) {
        visited[V] = true;

        System.out.print(V + " ");

        for (int i = 1; i <= N; i++) {
            if (arr[V][i] == 1 && !visited[i])
                dfs(i);
        }
    }
//after
  • ๋˜ํ•œ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋กœ ์ธํ•ด 2์ฐจ์› ๋ฐฐ์—ด arr๋ฅผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€๊ฒฝ
//before
...
	static int[][] arr;
	static int[] indegree;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		
		sc.nextLine();
		
		arr = new int[N+1][N+1];
		
		indegree = new int[N+1];
		
        for(int i=0; i<M; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            arr[from][to] = 1;
            indegree[to]++;
        }
		bfs();
	}
	
	public static void bfs(){
		Queue<Integer> queue = new LinkedList<>();
        List<Integer> result = new ArrayList<>();
        
        // ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€
        for (int i = 1; i <= N; i++) {
            if (indegree[i] == 0) {
                queue.add(i);
            }
        }
        
        while (!queue.isEmpty()) {
            int current = queue.poll();
            result.add(current); // ์ฒ˜๋ฆฌ๋œ ์ •์ ์„ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
            
            for (int i = 1; i <= N; i++) {
                if (arr[current][i] == 1) {
                    indegree[i]--; // ํ•ด๋‹น ์ •์ ์—์„œ ๋‚˜๊ฐ€๋Š” ๊ฐ„์„ ์„ ์ œ๊ฑฐ
                    if (indegree[i] == 0) {
                        queue.add(i);
                    }
                }
            }
        }
        
        // ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        for (int node : result) {
            System.out.print(node + " ");
        }
    }
}
//after
...
	 static List<Integer>[] List;
	
	static int[] indegree;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		
		//sc.nextLine(); -> ์ดˆ๊ธฐํ™”๋„ ์‚ญ์ œ!!
		
		//arr = new int[N+1][N+1];
		List = new ArrayList[N+1];
		indegree = new int[N+1];
		
		
		for (int i = 1; i <= N; i++) {
			List[i] = new ArrayList<>();
		}

        for(int i=0; i<M; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            //arr[from][to] = 1;
            List[from].add(to);
            indegree[to]++;
        }
		bfs();
	}
	
	public static void bfs(){
		//Queue<Integer> queue = new LinkedList<>();
    //List<Integer> result = new ArrayList<>();
		PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        // ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€
        for (int i = 1; i <= N; i++) {
            if (indegree[i] == 0) {
                pq.add(i);
            }
        }
        
        while (!pq.isEmpty()) {
            int n = pq.poll();
            System.out.print(n + " ");
            
            for (int i : List[n]) {
                indegree[i]--; // ํ•ด๋‹น ์ •์ ์—์„œ ๋‚˜๊ฐ€๋Š” ๊ฐ„์„ ์„ ์ œ๊ฑฐ
                    
                if (indegree[i] == 0) {
                    pq.add(i);
                }
            }
        }
    }
}


์ตœ์ข… ํ’€์ด


์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 1์‹œ๊ฐ„ 6๋ถ„(์ฒซ ํ’€์ด ์‹œ๊ฐ„ ํฌํ•จ)

  • ์ž…๋ ฅ
    • ํ•™์ƒ ์ˆ˜ N ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋น„๊ต ํšŸ์ˆ˜ M ์ž…๋ ฅ๋ฐ›๊ธฐ
    • 2์ฐจ์› ๋ฐฐ์—ด arr ์ž…๋ ฅ๋ฐ›๊ธฐ
  • ๊ณ„์‚ฐ
    • bfs ๊ณ„์‚ฐํ•˜๊ธฐ
  • ์ถœ๋ ฅ
    • ๊ฒฐ๊ณผ result ์ถœ๋ ฅํ•˜๊ธฐ
  • ๋ณ„๋„ ๋ฉ”์„œ๋“œ
    • BFS
      • ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์ง„ํ–‰ โ†’ ํ ์‚ฌ์šฉ
        • ํ ์„ ์–ธ
        • ํ add
        • ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋ฅผ true๋กœ ์„ค์ •
        • ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š๋Š” ๋™์•ˆ
          • ํ poll
          • ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ์ฐพ๊ธฐ
            • ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด โ†’ ํ์— ์ถ”๊ฐ€ + ๋ฐฉ๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝ
import java.util.*;

public class Main {
	
	static int N;
	static int M;
	
	//static int[][] arr;
	 static List<Integer>[] List;
	
	static int[] indegree;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		M = sc.nextInt();
		
		//sc.nextLine();
		
		//arr = new int[N+1][N+1];
		List = new ArrayList[N+1];
		indegree = new int[N+1];
		
		
		for (int i = 1; i <= N; i++) {
			List[i] = new ArrayList<>();
		}

    for(int i=0; i<M; i++) {
        int from = sc.nextInt();
        int to = sc.nextInt();
        //arr[from][to] = 1;
        List[from].add(to);
        indegree[to]++;
    }
		bfs();
	}
	
	public static void bfs(){
		//Queue<Integer> queue = new LinkedList<>();
    //List<Integer> result = new ArrayList<>();
		PriorityQueue<Integer> pq = new PriorityQueue<>();
        
        // ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€
        for (int i = 1; i <= N; i++) {
            if (indegree[i] == 0) {
                pq.add(i);
            }
        }
        
        while (!pq.isEmpty()) {
            int n = pq.poll();
            System.out.print(n + " ");
            
            for (int i : List[n]) {
                indegree[i]--; // ํ•ด๋‹น ์ •์ ์—์„œ ๋‚˜๊ฐ€๋Š” ๊ฐ„์„ ์„ ์ œ๊ฑฐ
                    
                if (indegree[i] == 0) {
                    pq.add(i);
                }
            }
        }
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€