From b289cf80c7988e8bc4a42ec1fbc3df7bb9d70f99 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Mon, 25 Nov 2024 00:22:23 +0900 Subject: [PATCH] =?UTF-8?q?solve=20:=204803=20=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/src/boj4803/Main.java | 91 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 java/src/boj4803/Main.java diff --git a/java/src/boj4803/Main.java b/java/src/boj4803/Main.java new file mode 100644 index 0000000..78c69de --- /dev/null +++ b/java/src/boj4803/Main.java @@ -0,0 +1,91 @@ +package boj4803; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.HashSet; +import java.util.Set; + +public class Main { + + private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + private static int[] parents; + private static boolean[] hasCycle; + + public static void main(String[] args) { + int t = 1; + + while (true) { + String line = readLine(); + if (line.equals("0 0")) { + break; + } + + String[] nm = line.split(" "); + int n = Integer.parseInt(nm[0]); + int m = Integer.parseInt(nm[1]); + + parents = new int[n + 1]; + hasCycle = new boolean[n + 1]; + for (int i = 1; i <= n; i++) { + parents[i] = i; + } + + for (int i = 0; i < m; i++) { + String[] elements = readLine().split(" "); + int a = Integer.parseInt(elements[0]); + int b = Integer.parseInt(elements[1]); + if (find(a) == find(b)) { + hasCycle[find(a)] = true; + } else { + union(a, b); + } + } + + Set roots = new HashSet<>(); + int treeCount = 0; + for (int i = 1; i <= n; i++) { + int root = find(i); + if (!roots.contains(root)) { + roots.add(root); + if (!hasCycle[root]) { + treeCount++; + } + } + } + + if (treeCount == 0) { + System.out.println("Case " + t + ": No trees."); + } else if (treeCount == 1) { + System.out.println("Case " + t + ": There is one tree."); + } else { + System.out.println("Case " + t + ": A forest of " + treeCount + " trees."); + } + + t++; + } + } + + private static void union(int x, int y) { + int rootX = find(x); + int rootY = find(y); + if (rootX != rootY) { + parents[rootY] = rootX; + } + } + + private static int find(int x) { + if (parents[x] != x) { + parents[x] = find(parents[x]); // 경로 압축 + } + return parents[x]; + } + + private static String readLine() { + try { + return reader.readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +}