From 1ee83d87e6c0ec41fd7b38f41fe33b3a5eb17b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A7=84?= Date: Mon, 10 Nov 2025 20:06:44 +0900 Subject: [PATCH] =?UTF-8?q?[20251110]=20PGM=20/=20LV2=20/=20=EC=A0=84?= =?UTF-8?q?=EB=A0=A5=EB=A7=9D=20=EB=91=98=EB=A1=9C=20=EB=82=98=EB=88=84?= =?UTF-8?q?=EA=B8=B0=20/=20=EA=B9=80=EB=AF=BC=EC=A7=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" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 "zinnnn37/202511/10 PGM LV2 \354\240\204\353\240\245\353\247\235 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" diff --git "a/zinnnn37/202511/10 PGM LV2 \354\240\204\353\240\245\353\247\235 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" "b/zinnnn37/202511/10 PGM LV2 \354\240\204\353\240\245\353\247\235 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" new file mode 100644 index 00000000..0bb85b2d --- /dev/null +++ "b/zinnnn37/202511/10 PGM LV2 \354\240\204\353\240\245\353\247\235 \353\221\230\353\241\234 \353\202\230\353\210\204\352\270\260.md" @@ -0,0 +1,66 @@ +```java +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class PGM_LV2_전력망_둘로_나누기 { + + private static int N, ans; + private static int[][] wires; + private static boolean[] visited; + private static List[] graph; + + private static void init(int n, int[][] wire) { + N = n; + ans = Integer.MAX_VALUE; + wires = wire; + + visited = new boolean[N + 1]; + + graph = new List[N + 1]; + for (int i = 0; i <= N; i++) { + graph[i] = new ArrayList<>(); + } + + for (int[] edge : wires) { + graph[edge[0]].add(edge[1]); + graph[edge[1]].add(edge[0]); + } + } + + private static void sol() { + int cnt; + int left; + for (int i = 0; i < N - 1; i++) { + Arrays.fill(visited, false); + + visited[wires[i][0]] = true; + visited[wires[i][1]] = true; + + cnt = exclude(wires[i][0], wires[i]); + + left = N - cnt; + ans = Math.min(ans, Math.abs(cnt - left)); + } + } + + private static int exclude(int start, int[] target) { + int cnt = 0; + + for (int n : graph[start]) { + if (visited[n]) continue; + + visited[n] = true; + cnt += exclude(n, target); + } + return cnt + 1; + } + + public int solution(int n, int[][] wires) { + init(n, wires); + sol(); + return ans; + } + +} +``` \ No newline at end of file