結果

問題 No.977 アリス仕掛けの摩天楼
ユーザー guriceringuricerin
提出日時 2020-02-01 10:26:27
言語 F#
(F# 4.0)
結果
WA  
実行時間 -
コード長 2,823 bytes
コンパイル時間 13,101 ms
コンパイル使用メモリ 210,984 KB
実行使用メモリ 50,612 KB
最終ジャッジ日時 2023-10-18 23:50:28
合計ジャッジ時間 16,892 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
30,960 KB
testcase_01 AC 61 ms
30,960 KB
testcase_02 AC 62 ms
30,960 KB
testcase_03 AC 62 ms
30,960 KB
testcase_04 AC 63 ms
30,968 KB
testcase_05 AC 62 ms
30,968 KB
testcase_06 AC 61 ms
30,960 KB
testcase_07 AC 60 ms
30,968 KB
testcase_08 AC 61 ms
30,976 KB
testcase_09 AC 61 ms
30,976 KB
testcase_10 WA -
testcase_11 AC 61 ms
30,984 KB
testcase_12 AC 61 ms
30,984 KB
testcase_13 AC 71 ms
33,148 KB
testcase_14 AC 69 ms
33,140 KB
testcase_15 AC 68 ms
33,140 KB
testcase_16 AC 69 ms
33,148 KB
testcase_17 AC 70 ms
33,148 KB
testcase_18 AC 86 ms
37,104 KB
testcase_19 WA -
testcase_20 AC 95 ms
40,632 KB
testcase_21 AC 112 ms
45,804 KB
testcase_22 AC 126 ms
49,732 KB
testcase_23 AC 127 ms
50,612 KB
testcase_24 AC 127 ms
50,612 KB
testcase_25 AC 127 ms
50,612 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (791 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
/home/judge/data/code/Main.fs(84,13): warning FS0025: この式のパターン マッチが不完全です たとえば、値 '[|_; _; _|]' はパターンに含まれないケースを示す可能性があります。 [/home/judge/data/code/main.fsproj]
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

open System
open System.Collections.Generic

[<AutoOpen>]
module Cin =
    let read f = stdin.ReadLine() |> f
    let reada f = stdin.ReadLine().Split() |> Array.map f
    let readChars() = read string |> Seq.toArray
    let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString()))

[<AutoOpen>]
module Cout =
    let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput()))
    let print (s: string) = writer.Write s
    let println (s: string) = writer.WriteLine s
    let inline puts (s: ^a) = string s |> println

type UnionFind =
    {
      /// 添字iが属するグループID
      par: int array
      /// 各集合の要素数
      size: int array }

[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module UnionFind =

    /// O(n)
    let init (n: int): UnionFind =
        let par = Array.init n id
        let size = Array.init n (fun _ -> 1)
        { UnionFind.par = par
          size = size }

    /// x: 0-indexed
    let rec root (x: int) (uf: UnionFind): int =
        let par = uf.par
        match x = par.[x] with
        | true -> x
        | false ->
            let px = par.[x]
            par.[x] <- root px uf
            par.[x]

    /// 連結判定
    let find (x: int) (y: int) (uf: UnionFind) = (root x uf) = (root y uf)

    let unite (x: int) (y: int) (uf: UnionFind): bool =
        let par, size = uf.par, uf.size
        let rx, ry = root x uf, root y uf
        match rx = ry with
        | true -> false
        | _ ->
            // マージテク(大きい方に小さい方を併合)
            let large, small =
                if size.[rx] < size.[ry] then ry, rx else rx, ry
            par.[small] <- large
            size.[large] <- size.[large] + size.[small]
            size.[small] <- 0
            true

    /// 素集合のサイズ
    /// x: 0-indexed
    let size (x: int) (uf: UnionFind): int =
        let rx = root x uf
        uf.size.[rx]

    /// 連結成分の個数
    /// O(n)
    let treeNum (uf: UnionFind): int =
        let par = uf.par
        let mutable cnt = 0
        par
        |> Array.iteri (fun i x ->
            if i = x then cnt <- cnt + 1)
        cnt

let main() =
    let n = read int
    let uf = UnionFind.init n
    let degs = Array.zeroCreate n
    for i in 0 .. n - 2 do
        let [| u; v |] = reada int
        UnionFind.unite u v uf |> ignore
        degs.[u] <- degs.[u] + 1
        degs.[v] <- degs.[v] + 1

    let num = uf |> UnionFind.treeNum
    let A = "Alice"
    let B = "Bob"
    if num >= 3 then
        A
    elif num = 2 then
        let ok = degs |> Array.tryFind (fun x -> x < 2)
        match ok with
        | Some _ -> A
        | _ -> B
    else
        B
    |> puts
    ()

main()
writer.Close()
0