提出 #4462059


ソースコード 拡げる

open Printf open Scanf
open Array

module UnionFind = struct
  let init n = Array.make n (-1)
  let is_root i a = a.(i) < 0
  let rec root_destructive x a =
    if a.(x) < 0 then x
    else (a.(x) <- root_destructive a.(x) a; a.(x))
  let size_destructive i a = let r = root_destructive i a in -a.(r)
  let same_destructive x y a = root_destructive x a = root_destructive y a
  let connect_destructive x y a =
    let p,q = root_destructive x a,root_destructive y a
    in if p = q then () else (
      let p,q = if a.(p) > a.(q) then q,p else p,q
      in
      a.(p) <- a.(p) + a.(q); a.(q) <- p)
end module UF = UnionFind
let rev a = a |> to_list |> List.rev |> of_list

let () = scanf " %d %d" @@ fun n m ->
  let uf = UF.init n in
  let q = rev @@
    init m (fun _ -> scanf " %d %d" @@ fun a b -> a-1,b-1)
  in
  let d = map (fun (a,b) ->
    if not @@ UF.same_destructive a b uf then (
      let p,q =
        UF.size_destructive a uf,
        UF.size_destructive b uf in
      UF.connect_destructive a b uf;
      p*q
    ) else 0
  ) q |> rev in
  ignore @@ fold_left (fun sum v ->
    let w = sum + v in
    printf "%d\n" w; w
  ) 0 d


提出情報

提出日時
問題 D - Decayed Bridges
ユーザ sk0
言語 OCaml (4.02.3)
得点 400
コード長 1187 Byte
結果 AC
実行時間 106 ms
メモリ 15616 KiB

ジャッジ結果

セット名 All Sample
得点 / 配点 400 / 400 0 / 0
結果
AC × 26
AC × 3
セット名 テストケース
All sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15, testcase_16, testcase_17, testcase_18, testcase_19, testcase_20, testcase_21, testcase_22, testcase_23
Sample sample_01, sample_02, sample_03
ケース名 結果 実行時間 メモリ
sample_01 AC 1 ms 384 KiB
sample_02 AC 1 ms 384 KiB
sample_03 AC 1 ms 384 KiB
testcase_01 AC 1 ms 384 KiB
testcase_02 AC 76 ms 12160 KiB
testcase_03 AC 96 ms 12544 KiB
testcase_04 AC 105 ms 15616 KiB
testcase_05 AC 105 ms 15616 KiB
testcase_06 AC 1 ms 384 KiB
testcase_07 AC 86 ms 12928 KiB
testcase_08 AC 57 ms 9856 KiB
testcase_09 AC 1 ms 384 KiB
testcase_10 AC 104 ms 15616 KiB
testcase_11 AC 19 ms 6400 KiB
testcase_12 AC 1 ms 384 KiB
testcase_13 AC 104 ms 15616 KiB
testcase_14 AC 106 ms 15616 KiB
testcase_15 AC 84 ms 11708 KiB
testcase_16 AC 38 ms 7040 KiB
testcase_17 AC 87 ms 13952 KiB
testcase_18 AC 84 ms 13184 KiB
testcase_19 AC 83 ms 13056 KiB
testcase_20 AC 84 ms 13184 KiB
testcase_21 AC 1 ms 384 KiB
testcase_22 AC 12 ms 5504 KiB
testcase_23 AC 22 ms 6400 KiB