let is_bipartite n adj =
  let color = Array.make n (-1) in
  let ok = ref true in
  let q = Queue.create () in
  for src = 0 to n - 1 do
    if color.(src) = -1 then begin
      color.(src) <- 0;
      Queue.push src q;
      while not (Queue.is_empty q) do
        let u = Queue.pop q in
        List.iter (fun v ->
          if color.(v) = -1 then begin
            color.(v) <- 1 - color.(u);
            Queue.push v q
          end else if color.(v) = color.(u) then
            ok := false
        ) adj.(u)
      done
    end
  done;
  let zeros = ref 0 in
  for i = 0 to n - 1 do
    if color.(i) = 0 then zeros := !zeros + 1
  done;
  if !ok then !zeros else -1

;;

let n = 7 in
let adj = [|
  [1; 3];
  [0; 2; 4];
  [1; 5];
  [0; 4; 6];
  [1; 3];
  [2; 6];
  [3; 5]
|] in
is_bipartite n adj
