View

๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ

๊ทธ๋ž˜ํ”„๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ์™€ BFS๋กœ ํƒ์ƒ‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ •์ ์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” ์ •์  ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๋” ์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์ ์ด ์—†๋Š” ๊ฒฝ์šฐ ์ข…๋ฃŒํ•œ๋‹ค. ์ •์  ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000), ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 10,000), ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ์ •์ ์˜ ๋ฒˆํ˜ธ V๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ฐ„์„ ์€ ์–‘๋ฐฉํ–ฅ์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— DFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ, ๊ทธ ๋‹ค์Œ ์ค„์—๋Š” BFS๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. V๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ๋œ ์ ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

5 5 3
5 4
5 2
1 2
3 4
3 1

์˜ˆ์ œ ์ถœ๋ ฅ 1

3 1 2 5 4
3 1 4 2 5


๐Ÿคฆ‍โ™€๏ธ My Solution

package WS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;

public class BJ_1260 {

    static int N, M, V;
    static int[][] link;
    static boolean[] check;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        V = Integer.parseInt(st.nextToken());
        link = new int[1001][1001];

        int x, y;
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            x = Integer.parseInt(st.nextToken());
            y = Integer.parseInt(st.nextToken());
            link[y][x] = link[x][y] = 1; // ๊ฐ„์„  ์—ฐ๊ฒฐ ์ƒํƒœ

        }
        check = new boolean[1001];
        DFS();
        check = new boolean[1001];
        System.out.println();
        BFS();
    }

    static void BFS() {
        Queue<Integer> que = new ArrayDeque<>();
        que.add(V);// ์‹œ์ž‘์  ๋„ฃ์Œ
        check[V] = true;
        System.out.print(V + " "); // ์‹œ์ž‘์  ์ถœ๋ ฅ

        while (!que.isEmpty()) { // ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„๋•Œ๊นŒ์ง€
            int node = que.poll(); // ๋งจ์ฒ˜์Œ: ์‹œ์ž‘๊ฐ’์„ ๊บผ๋‚ด์˜ด

            for (int i = 1; i <= N; i++) {
                if (link[i][node] == 1 && check[i] == false) {
                    que.add(i);
                    System.out.print(i + " ");
                    check[i] = true;
                }

            }
        }

    }

    static void DFS() {
        Stack<Integer> que = new Stack<>();
        que.push(V);// ์‹œ์ž‘์  ๋„ฃ์Œ

        while (!que.isEmpty()) { // ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„๋•Œ๊นŒ์ง€
            int node = que.pop(); // ๋งจ์ฒ˜์Œ: ์‹œ์ž‘๊ฐ’์„ ๊บผ๋‚ด์˜ด
            if (check[node])
                continue;
            check[node] = true;
            System.out.print(node + " ");

            for (int i = N; i >= 1; i--) {
                if (link[i][node] == 1 && check[i] == false) {
                    que.push(i);

                }
            }
        }

    }
}


์ฐธ๊ณ : https://m.blog.naver.com/lm040466/221787478911

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30