Skip to content

Commit

Permalink
solved 11725
Browse files Browse the repository at this point in the history
  • Loading branch information
sangwoo-joh committed Jul 8, 2024
1 parent 6737390 commit 3b09287
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions boj/11725.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Graph = struct
module Edge = Set.Make (Int)

type t = (int, Edge.t) Hashtbl.t

let create () : t = Hashtbl.create 16

let add_edge (g : t) src snk =
match Hashtbl.find_opt g src with
| Some edge -> Hashtbl.replace g src (Edge.add snk edge)
| None -> Hashtbl.add g src (Edge.singleton snk)
;;

let dfs_iter g src ~(parent : Edge.elt option) ~f =
let visited = Hashtbl.create 16 in
let has_visited x =
match Hashtbl.find_opt visited x with
| None -> false
| Some _ -> true
in
let visit ~parent x =
f parent x;
Hashtbl.add visited x None
in
let rec helper ~parent src =
if has_visited src
then ()
else (
visit ~parent src;
match Hashtbl.find_opt g src with
| None -> ()
| Some edge -> Edge.iter (helper ~parent:(Some src)) edge)
in
helper ~parent src
;;
end

let load () =
let n = Scanf.scanf "%d " Fun.id in
let g = Graph.create () in
for _ = 1 to n - 1 do
let src, snk = Scanf.scanf "%d %d " (fun x y -> x, y) in
Graph.add_edge g src snk;
Graph.add_edge g snk src
done;
g, n
;;

let () =
let graph, n = load () in
let parent_map = Hashtbl.create 16 in
Graph.dfs_iter
graph
~parent:None
~f:(fun parent src ->
match parent with
| None -> ()
| Some p -> Hashtbl.add parent_map src p)
1;
for s = 2 to n do
Printf.printf "%d\n" (Hashtbl.find parent_map s)
done
;;

0 comments on commit 3b09287

Please sign in to comment.