peacekim
할 수 있는 것과 할 수 없는 것.
peacekim
전체 방문자
오늘
어제
  • 분류 전체보기 (68)
    • 👨‍🏫ps (44)
      • ❄️프로그래머스 (20)
      • 🔟0️⃣백준 (21)
      • leetcode (3)
    • ✍🏻study (20)
      • 👐java (6)
      • 🍃spring (1)
      • 🥇algorithm (0)
      • 🚘oodp (4)
      • 📒 jpa (3)
      • 👣DB (2)
      • 🌂네트워크 (0)
      • 🎸기타 (3)
      • 👊 kotlin (1)
      • 🫥 jvm (0)
    • 📽project (4)
      • 🎀ReBoN (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
peacekim

할 수 있는 것과 할 수 없는 것.

[백준] 11724번 연결 요소의 개수 풀어보기 [백준]
👨‍🏫ps/🔟0️⃣백준

[백준] 11724번 연결 요소의 개수 풀어보기 [백준]

2022. 3. 4. 15:34
반응형

그래프가 몇개 인지 구하는 문제였다. 

나는 DFS로 풀었는데, Union-Find로 풀면 더 빠르게 풀 수 있었을 거 같다,,,

 

👨‍🏫 풀이

dfs로 탐색한다. 한번 dfs가 끝날때마다 graph가 하나 있는 것이다.

 

👨🏻‍💻 코드

package 번11724;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class Main {
    static int N, M;
    static HashMap<Integer,ArrayList<Integer>> graph = new HashMap<>();
    static boolean[] isVisited;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] nums = Arrays.stream(br.readLine().split(" ")).mapToInt(i -> Integer.parseInt(i)).toArray();
        N = nums[0]; M = nums[1];

        isVisited = new boolean[N+1];
        Arrays.fill(isVisited, false);

        for(int i = 0 ; i < M ; i++){
            int[] nodes = Arrays.stream(br.readLine().split(" ")).mapToInt(n -> Integer.parseInt(n)).toArray();
            if(graph.get(nodes[0]) == null){
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(nodes[1]);
                graph.put(nodes[0],tmp);
            } else {
                graph.get(nodes[0]).add(nodes[1]);
            }
            if(graph.get(nodes[1]) == null){
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(nodes[0]);
                graph.put(nodes[1],tmp);
            } else {
                graph.get(nodes[1]).add(nodes[0]);
            }
        }
        int ans = 0;
        for(int i = 1 ; i <= N ; i++){
            if(isVisited[i]) continue;
            ans++;
            dfs(i);
        }
        System.out.println(ans);
    }

    private static void dfs(int cur) {
        ArrayList<Integer> tmp = graph.get(cur);
        if(tmp == null) return;
        for(int i = 0 ; i < tmp.size() ; i++){
            int next = tmp.get(i);
            if(isVisited[next]) continue;
            isVisited[next] = true;
            dfs(next);
        }
    }
}
반응형

'👨‍🏫ps > 🔟0️⃣백준' 카테고리의 다른 글

[백준] 1261번 알고스팟 풀어보기 [Java]  (0) 2022.03.07
[백준] 16929번 Two Dots 풀어보기 [Java]  (0) 2022.03.06
[백준] 15661번 링크와 스타트 풀어보기 [Java]  (0) 2022.03.04
[백준] 1759번 암호 만들기 풀어보기 [Java]  (0) 2022.03.04
[백준] 1748번 수 이어 쓰기 1 [Java]  (0) 2022.03.02
    '👨‍🏫ps/🔟0️⃣백준' 카테고리의 다른 글
    • [백준] 1261번 알고스팟 풀어보기 [Java]
    • [백준] 16929번 Two Dots 풀어보기 [Java]
    • [백준] 15661번 링크와 스타트 풀어보기 [Java]
    • [백준] 1759번 암호 만들기 풀어보기 [Java]
    peacekim
    peacekim
    할 수 있는 것과 할 수 없는 것. github: https://github.com/PyeongGangKim

    티스토리툴바