From 4823219fb4a170f79ab629e068ecba454a80b5cd Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 15 Aug 2025 22:54:10 +0900 Subject: [PATCH] =?UTF-8?q?[20250815]=20BOJ=20/=20G5=20/=20=EC=A7=91?= =?UTF-8?q?=ED=95=A9=EC=9D=98=20=ED=91=9C=ED=98=84=20/=20=EC=9D=B4?= =?UTF-8?q?=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\354\235\230 \355\221\234\355\230\204.md" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "lkhyun/202508/15 BOJ G5 \354\247\221\355\225\251\354\235\230 \355\221\234\355\230\204.md" diff --git "a/lkhyun/202508/15 BOJ G5 \354\247\221\355\225\251\354\235\230 \355\221\234\355\230\204.md" "b/lkhyun/202508/15 BOJ G5 \354\247\221\355\225\251\354\235\230 \355\221\234\355\230\204.md" new file mode 100644 index 00000000..8b99b25b --- /dev/null +++ "b/lkhyun/202508/15 BOJ G5 \354\247\221\355\225\251\354\235\230 \355\221\234\355\230\204.md" @@ -0,0 +1,59 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N,M; + static int[] root; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + root = new int[N+1]; + + for (int i = 0; i <= N; i++) { + root[i] = i; + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + + int cmd = Integer.parseInt(st.nextToken()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + if(cmd == 0){ //합 + union(a,b); + }else if(cmd == 1){ //같이 있나? + if(find(a) == find(b)){ + bw.write("yes\n"); + }else{ + bw.write("no\n"); + } + } + } + + bw.close(); + } + + static int find(int cur){ + if(cur == root[cur]) return cur; + else{ + return root[cur] = find(root[cur]); + } + } + static void union(int a, int b){ + int rootA = find(a); + int rootB = find(b); + + if(rootA < rootB){ + root[rootB] = rootA; + }else{ + root[rootA] = rootB; + } + } +} +```