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; + } + } +} + +```