From b1412f9662197dee589afc1a6070c7f431b0f0e3 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Fri, 26 Sep 2025 19:32:50 +0900 Subject: [PATCH] =?UTF-8?q?[20250926]=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=EA=B9=80=EC=88=98=EC=97=B0?= 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" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "suyeun84/202509/26 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/suyeun84/202509/26 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/suyeun84/202509/26 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..d3edf200 --- /dev/null +++ "b/suyeun84/202509/26 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,40 @@ +```java +import java.util.*; +class Solution { + static int N; + static List> graph; + public int solution(int n, int[][] wires) { + int answer = Integer.MAX_VALUE; + N = n; + graph = new ArrayList<>(); + for (int i = 0; i <= n; i++) graph.add(new ArrayList<>()); + for (int i = 0; i < n-1; i++) { + graph.get(wires[i][0]).add(wires[i][1]); + graph.get(wires[i][1]).add(wires[i][0]); + } + for (int i = 0; i < n-1; i++) { + answer = Math.min(answer, check(wires[i][0], wires[i][1])); + } + + return answer; + } + + private int check(int a, int b) { + Queue q = new LinkedList<>(); + boolean[] visited = new boolean[N+1]; + int cnt = 1; + visited[1] = true; + q.add(1); + while(!q.isEmpty()) { + int curr = q.poll(); + for (int next : graph.get(curr)) { + if (visited[next] || (a==curr && b==next) || (a==next && b==curr)) continue; + cnt++; + visited[next] = true; + q.add(next); + } + } + return Math.abs((N-cnt) - cnt); + } +} +```