From d127d3e84dc10cea4a3c9472fe4f4e2f3ebb2329 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Sun, 22 Feb 2026 16:17:23 +0900 Subject: [PATCH] =?UTF-8?q?[20260222]=20BOJ=20/=20G3=20/=20=EB=B6=88?= =?UTF-8?q?=EC=9A=B0=EC=9D=B4=EC=9B=83=EB=8F=95=EA=B8=B0=20/=20=EC=9D=B4?= =?UTF-8?q?=EC=A4=80=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\354\233\203\353\217\225\352\270\260.md" | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 "JHLEE325/202602/22 BOJ G3 \353\266\210\354\232\260\354\235\264\354\233\203\353\217\225\352\270\260.md" diff --git "a/JHLEE325/202602/22 BOJ G3 \353\266\210\354\232\260\354\235\264\354\233\203\353\217\225\352\270\260.md" "b/JHLEE325/202602/22 BOJ G3 \353\266\210\354\232\260\354\235\264\354\233\203\353\217\225\352\270\260.md" new file mode 100644 index 00000000..a6329ad6 --- /dev/null +++ "b/JHLEE325/202602/22 BOJ G3 \353\266\210\354\232\260\354\235\264\354\233\203\353\217\225\352\270\260.md" @@ -0,0 +1,88 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int[] parent; + + static class Edge implements Comparable { + int u, v, w; + Edge(int u, int v, int w) { + this.u = u; + this.v = v; + this.w = w; + } + @Override + public int compareTo(Edge o) { + return this.w - o.w; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + int N = Integer.parseInt(br.readLine()); + + List edges = new ArrayList<>(); + long totalLength = 0; + + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < N; j++) { + char c = line.charAt(j); + int len = charToInt(c); + + if (len > 0) { + totalLength += len; + if (i != j) { + edges.add(new Edge(i, j, len)); + } + } + } + } + + Collections.sort(edges); + + parent = new int[N]; + for (int i = 0; i < N; i++) parent[i] = i; + + int useEdgeCount = 0; + long mstSum = 0; + + for (Edge edge : edges) { + if (union(edge.u, edge.v)) { + mstSum += edge.w; + useEdgeCount++; + } + } + + if (useEdgeCount == N - 1) { + System.out.println(totalLength - mstSum); + } else { + System.out.println(-1); + } + } + + static int charToInt(char c) { + if (c >= 'a' && c <= 'z') return c - 'a' + 1; + if (c >= 'A' && c <= 'Z') return c - 'A' + 27; + return 0; + } + + static int find(int x) { + if (parent[x] == x) return x; + return parent[x] = find(parent[x]); + } + + static boolean union(int x, int y) { + int rootX = find(x); + int rootY = find(y); + if (rootX != rootY) { + parent[rootX] = rootY; + return true; + } + return false; + } +} +```