반응형

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

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int N, M, answer = 0;
    static boolean[] visited;
    static boolean[][] graph;

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

        for (int i = 1; i <= N; i++) {
            if (graph[idx][i] && !visited[i]) {
                dfs(i);
            }
        }
    }
    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());

        graph = new boolean[N + 1][N + 1];
        visited = new boolean[N + 1];

        for (int i = 1; i <= M; i++) {
            st = new StringTokenizer(br.readLine());
            int u = Integer.parseInt(st.nextToken());
            int v = Integer.parseInt(st.nextToken());

            graph[u][v] = true;
            graph[v][u] = true;

        }

        for (int j = 1; j <= N; j++) {
            if(!visited[j]) {
                dfs(j);
                answer++;
            }
        }

        System.out.print(answer);
    }
}

핵심

1. DFS를 언제 수행할지

2. graph, 재귀

반응형
반응형

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

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    static int N, M, V;
    static boolean[] visited;
    static boolean[][] graph;
    static StringBuilder sb = new StringBuilder();
    static Queue<Integer> q = new LinkedList<>();

    public static void bfs(int start) {
        q.add(start);
        visited[start] = true;

        while (!q.isEmpty()) {

            start = q.poll();
            sb.append(start).append(" ");

            for (int i = 1; i <= N; i++) {
                if (graph[start][i] && !visited[i]) {
                        q.add(i);
                        visited[i] = true;
                }

            }
        }
    }

    public static void dfs(int start) {
        visited[start] = true;
        sb.append(start).append(" ");

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

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

        // 1. 입력 먼저

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        V = Integer.parseInt(st.nextToken());

        visited = new boolean[N + 10];
        graph = new boolean[N + 10][N + 10];

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());

            int y = Integer.parseInt(st.nextToken());
            int x = Integer.parseInt(st.nextToken());

            graph[y][x] = true;
            graph[x][y] = true;
        }

        dfs(V);
        System.out.println(sb.toString());
        sb = new StringBuilder();
        visited = new boolean[N + 10];
        bfs(V);
        System.out.println(sb.toString());
    }
}

 


핵심

1. DFS는 재귀 / BFS는 Queue

2. graph, visited, Queue(BFS)를 활용

반응형

+ Recent posts