From 35e70b9dc95422587667a8f342a64662ffb4e9b6 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 31 Oct 2025 20:20:55 +0900 Subject: [PATCH] =?UTF-8?q?[20251031]=20PGM=20/=20Lv2=20/=20=EC=A0=84?= =?UTF-8?q?=EB=A0=A5=EB=A7=9D=EC=9D=84=20=EB=91=98=EB=A1=9C=20=EB=82=98?= =?UTF-8?q?=EB=88=84=EA=B8=B0=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \353\202\230\353\210\204\352\270\260.md" | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 "lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" diff --git "a/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" "b/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" new file mode 100644 index 00000000..45362f48 --- /dev/null +++ "b/lkhyun/202510/31 PGM Lv2 \354\240\204\353\240\245\353\247\235\354\235\204 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" @@ -0,0 +1,42 @@ +```java +import java.util.*; +class Solution { + static List[] adjList; + public int solution(int n, int[][] wires) { + int answer = n; + adjList = new List[n+1]; + for(int i = 0; i<=n; i++){ + adjList[i] = new ArrayList<>(); + } + for(int[] wire : wires){ + adjList[wire[0]].add(wire[1]); + adjList[wire[1]].add(wire[0]); + } + for(int[] wire : wires){ + answer = Math.min(answer,Math.abs(n-(2*BFS(n, wire[0],wire[1])))); + } + + return answer; + } + public int BFS(int n, int start, int except){ + int count = 0; + ArrayDeque q = new ArrayDeque<>(); + boolean[] visited = new boolean[n+1]; + q.offer(start); + visited[start] = true; + count++; + + while(!q.isEmpty()){ + int cur = q.poll(); + + for(int next : adjList[cur]){ + if(visited[next] || next == except) continue; + q.offer(next); + visited[next] = true; + count++; + } + } + return count; + } +} +```