From ada0d8ee08001ca06fbe7bf818e350f9868f3fe4 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Wed, 24 Sep 2025 20:22:30 +0900 Subject: [PATCH] =?UTF-8?q?[20250924]=20BOJ=20/=20G2=20/=20=EB=8B=AD?= =?UTF-8?q?=EC=8B=B8=EC=9B=80=20=ED=8C=80=20=EC=A0=95=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\200 \354\240\225\355\225\230\352\270\260" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "0224LJH/202509/24 BOJ \353\213\255\354\213\270\354\233\200 \355\214\200 \354\240\225\355\225\230\352\270\260" diff --git "a/0224LJH/202509/24 BOJ \353\213\255\354\213\270\354\233\200 \355\214\200 \354\240\225\355\225\230\352\270\260" "b/0224LJH/202509/24 BOJ \353\213\255\354\213\270\354\233\200 \355\214\200 \354\240\225\355\225\230\352\270\260" new file mode 100644 index 00000000..58401346 --- /dev/null +++ "b/0224LJH/202509/24 BOJ \353\213\255\354\213\270\354\233\200 \355\214\200 \354\240\225\355\225\230\352\270\260" @@ -0,0 +1,103 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +class Main { + static BufferedReader br= new BufferedReader(new InputStreamReader(System.in));; + static int nodeCnt,relaCnt; + static int[] parent,enemy; + static HashSet ans = new HashSet<>(); + + static final String FRIEND = "F"; + static final String ENEMY = "E"; + + static class Node{ + int rival = -1; + + } + + + public static void main(String[] args) throws NumberFormatException, IOException { + init(); + process(); + print(); + + } + + + public static void init() throws NumberFormatException, IOException { + nodeCnt = Integer.parseInt(br.readLine()); + relaCnt = Integer.parseInt(br.readLine()); + enemy = new int[nodeCnt+1]; + parent = new int[nodeCnt+1]; + + for (int i = 1 ; i <= nodeCnt; i++) { + parent[i] = i; + } + } + + public static void process() throws IOException { + for (int i = 0; i < relaCnt; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + String rela = st.nextToken(); + int node1 = Integer.parseInt(st.nextToken()); + int node2 = Integer.parseInt(st.nextToken()); + + if (rela.equals(FRIEND)) { + + union(node1,node2); + } else { + if (enemy[node1] != 0) { + union(node2, enemy[node1]); + enemy[node1] = Math.min(enemy[node1], node2); + } + else enemy[node1] = node2; + + if (enemy[node2] != 0) { + + union(node1, enemy[node2]); + enemy[node2] = Math.min(enemy[node2], node1); + + } + else enemy[node2] = node1; + } + + } + + for (int i = 1; i <= nodeCnt; i++) { + ans.add(find(i)); + } + + } + + public static void union(int node1, int node2) { + + int parent1 = find(node1); + int parent2 = find(node2); + if (parent1 <= parent2) { + parent[node2] = parent1; + } else { + parent[node1] = parent2; + } + } + + public static int find(int node) { + if (parent[node] == node) { + return node; + } + + return parent[node] = find(parent[node]); + } + + + + + + + public static void print() { + System.out.println(ans.size()); + } +} +```