結果

問題 No.277 根掘り葉掘り
ユーザー pekempeypekempey
提出日時 2017-01-27 21:46:51
言語 F#
(F# 4.0)
結果
AC  
実行時間 654 ms / 3,000 ms
コード長 742 bytes
コンパイル時間 6,046 ms
コンパイル使用メモリ 162,032 KB
実行使用メモリ 38,148 KB
最終ジャッジ日時 2023-08-25 03:49:18
合計ジャッジ時間 13,966 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
22,264 KB
testcase_01 AC 61 ms
22,168 KB
testcase_02 AC 61 ms
22,136 KB
testcase_03 AC 62 ms
22,260 KB
testcase_04 AC 63 ms
22,252 KB
testcase_05 AC 62 ms
22,268 KB
testcase_06 AC 64 ms
24,260 KB
testcase_07 AC 63 ms
24,232 KB
testcase_08 AC 61 ms
22,176 KB
testcase_09 AC 654 ms
35,000 KB
testcase_10 AC 613 ms
38,148 KB
testcase_11 AC 633 ms
33,536 KB
testcase_12 AC 642 ms
37,520 KB
testcase_13 AC 642 ms
37,596 KB
testcase_14 AC 642 ms
37,584 KB
testcase_15 AC 648 ms
37,640 KB
testcase_16 AC 642 ms
37,452 KB
testcase_17 AC 643 ms
37,616 KB
testcase_18 AC 648 ms
35,516 KB
testcase_19 AC 633 ms
35,544 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

diff #

let N = System.Console.ReadLine() |> int
let graph : (int list) array = Array.create N []

for _ in 0 .. N - 2 do
    let u, v = System.Console.ReadLine().Split() |> Array.map int |> fun a -> a.[0], a.[1]
    let u = u - 1
    let v = v - 1
    graph.[u] <- v :: graph.[u]
    graph.[v] <- u :: graph.[v]

let q = System.Collections.Generic.Queue<int>();

let dist = Array.create N (-1)
dist.[0] <- 0
q.Enqueue(0)

for i = 1 to N - 1 do
    if List.length graph.[i] = 1 then
        dist.[i] <- 0
        q.Enqueue(i)

while q.Count > 0 do
    let u = q.Dequeue()
    for v in graph.[u] do
        if dist.[v] = -1 then
            dist.[v] <- dist.[u] + 1
            q.Enqueue(v)

dist |> Array.iter (fun x -> System.Console.WriteLine(x))

0