Scanf.scanf "%d %d" (fun n q ->
    let par = Array.init (n + 1) (fun i -> i) in
    let rec root x =
        if x = par.(x) then x else
            let r = root par.(x) in
            let () = par.(x) <- r in
            r
    in
    let merge a b =
        if root a <> root b then par.(par.(b)) <- par.(a)
    in
    for i = 1 to q do
        Scanf.scanf " %d %d %d" (fun t u v ->
            if t = 0 then merge u v else
                Printf.printf "%d\n" @@ if root u = root v then 1 else 0
        )
    done
)