結果

問題 No.1103 Directed Length Sum
ユーザー yuly3yuly3
提出日時 2020-07-28 00:56:24
言語 Nim
(2.0.2)
結果
AC  
実行時間 1,511 ms / 3,000 ms
コード長 957 bytes
コンパイル時間 6,141 ms
コンパイル使用メモリ 69,460 KB
実行使用メモリ 141,568 KB
最終ジャッジ日時 2023-09-11 06:15:12
合計ジャッジ時間 17,878 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,264 KB
testcase_01 AC 5 ms
12,252 KB
testcase_02 AC 880 ms
133,444 KB
testcase_03 AC 796 ms
140,320 KB
testcase_04 AC 831 ms
92,872 KB
testcase_05 AC 1,511 ms
141,568 KB
testcase_06 AC 515 ms
66,388 KB
testcase_07 AC 105 ms
24,952 KB
testcase_08 AC 174 ms
31,432 KB
testcase_09 AC 64 ms
21,264 KB
testcase_10 AC 247 ms
38,996 KB
testcase_11 AC 901 ms
101,180 KB
testcase_12 AC 526 ms
65,880 KB
testcase_13 AC 254 ms
39,284 KB
testcase_14 AC 46 ms
19,216 KB
testcase_15 AC 405 ms
52,352 KB
testcase_16 AC 1,025 ms
114,544 KB
testcase_17 AC 1,039 ms
108,572 KB
testcase_18 AC 246 ms
37,008 KB
testcase_19 AC 938 ms
100,744 KB
testcase_20 AC 78 ms
23,524 KB
testcase_21 AC 149 ms
29,664 KB
testcase_22 AC 717 ms
81,820 KB
testcase_23 AC 431 ms
56,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, math, algorithm

const MOD = 10 ^ 9 + 7
var
    graph: seq[seq[int]]
    parents, depth, dp: array[1000010, int]
    stack0, stack1: seq[int]


proc solve() =
    let N = stdin.readLine.parseInt
    graph = newSeqWith(N, newSeq[int]())
    parents.fill(-1)
    var a, b: int
    for _ in 0..<N - 1:
        (a, b) = stdin.readLine.split.map(parseInt)
        a -= 1; b -= 1
        graph[a].add(b)
        parents[b] = a

    let root = find(parents, -1)
    stack0 = newSeq[int]()
    stack0.add(root)
    var cur: int
    while stack0.len != 0:
        cur = stack0.pop()
        for child in graph[cur]:
            depth[child] = depth[cur] + 1
            stack0.add(child)
            stack1.add(child)
    
    for cur in stack1.reversed():
        dp[parents[cur]] += dp[cur] + 1
    
    var ans = 0
    for n in 0..<N:
        ans = (ans + depth[n] * (dp[n] + 1)) mod MOD
    echo ans


when is_main_module:
    solve()
0