diff --git "a/SeongHoonC/\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234/\354\247\221\355\225\251\354\235\230\355\221\234\355\230\204.kt" "b/SeongHoonC/\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234/\354\247\221\355\225\251\354\235\230\355\221\234\355\230\204.kt" new file mode 100644 index 00000000..02c5fdd2 --- /dev/null +++ "b/SeongHoonC/\354\234\240\353\213\210\354\230\250\355\214\214\354\235\270\353\223\234/\354\247\221\355\225\251\354\235\230\355\221\234\355\230\204.kt" @@ -0,0 +1,43 @@ +import java.io.BufferedReader +import java.io.InputStreamReader + +private lateinit var br: BufferedReader +private lateinit var parents: Array + +fun main() { + br = BufferedReader(InputStreamReader(System.`in`)) + val (n, m) = br.readLine().split(" ").map { it.toInt() } + parents = Array(n + 1) { it } + + repeat(m) { + val (command, a, b) = br.readLine().split(" ").map { it.toInt() } + if (command == 0) { + union(a, b) + } else { + println(if (findParents(a) == findParents(b)) "YES" else "NO") + } + } +} + +private fun findParents(a: Int): Int { + var parent = parents[a] + while (parents[parent] != parent) { + parent = parents[parent] + } + parents[a] = parent + return parent +} + +private fun union(a: Int, b: Int) { + val parentA = findParents(a) + val parentB = findParents(b) + + if (parentA == parentB) { + return + } + if (parentA > parentB) { + parents[parentA] = parentB + return + } + parents[parentB] = parentA +} \ No newline at end of file