let n = 6
let adj = [|
  [1; 2];
  [3];
  [3; 4];
  [5];
  [5];
  []
|]

let visited = Array.make n false
let order = ref []

let rec dfs v =
  if not visited.(v) then begin
    visited.(v) <- true;
    List.iter dfs adj.(v);
    order := v :: !order
  end

;;

for v = 0 to n - 1 do
  dfs v
done;
List.fold_left (fun acc v -> acc * 10 + v) 0 !order
