let file = FileIO()
let n = file.readInt(), m = file.readInt()
var arr = Array(0...n)
var answer = ""
for _ in 0..<m {
let type = file.readInt()
let a = file.readInt()
let b = file.readInt()
if type == 0 {
union(a, b)
} else {
if getParents(a, b) {
answer += "YES\n"
} else {
answer += "NO\n"
}
}
}
print(answer)
func union(_ a: Int, _ b: Int) {
let aParent = find(a)
let bParent = find(b)
if a < b {
arr[bParent] = aParent
} else {
arr[aParent] = bParent
}
}
func find(_ num: Int) -> Int {
if arr[num] == num {
return num
}
arr[num] = find(arr[num])
return arr[num]
}
func getParents(_ a: Int, _ b: Int) -> Bool {
let aParent = find(a)
let bParent = find(b)
return aParent == bParent
}