From c79a0c862b5be987eaca213a3667e22261d53330 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Sat, 19 Jul 2025 23:22:35 +0900 Subject: [PATCH] =?UTF-8?q?[20250719]=20BOJ=20/=20G4=20/=20=EC=97=AC?= =?UTF-8?q?=ED=96=89=20=EA=B0=80=EC=9E=90=20/=20=EC=84=A4=EC=A7=84?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\355\226\211 \352\260\200\354\236\220.md" | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 "Seol-JY/202507/19 G4 \354\227\254\355\226\211 \352\260\200\354\236\220.md" diff --git "a/Seol-JY/202507/19 G4 \354\227\254\355\226\211 \352\260\200\354\236\220.md" "b/Seol-JY/202507/19 G4 \354\227\254\355\226\211 \352\260\200\354\236\220.md" new file mode 100644 index 00000000..b40fd9a7 --- /dev/null +++ "b/Seol-JY/202507/19 G4 \354\227\254\355\226\211 \352\260\200\354\236\220.md" @@ -0,0 +1,58 @@ +```java +import java.util.*; + +public class Main { + static int[] parent; + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + int M = sc.nextInt(); + + parent = new int[N + 1]; + for (int i = 1; i <= N; i++) { + parent[i] = i; + } + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= N; j++) { + int connected = sc.nextInt(); + if (connected == 1) { + union(i, j); + } + } + } + + int[] plan = new int[M]; + for (int i = 0; i < M; i++) { + plan[i] = sc.nextInt(); + } + + int root = find(plan[0]); + boolean possible = true; + for (int i = 1; i < M; i++) { + if (find(plan[i]) != root) { + possible = false; + break; + } + } + + System.out.println(possible ? "YES" : "NO"); + } + + static int find(int x) { + if (x != parent[x]) { + parent[x] = find(parent[x]); + } + return parent[x]; + } + + static void union(int a, int b) { + int rootA = find(a); + int rootB = find(b); + if (rootA != rootB) { + parent[rootB] = rootA; + } + } +} + +```