結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,088 KB
testcase_01 AC 2 ms
5,080 KB
testcase_02 AC 1,160 ms
214,484 KB
testcase_03 AC 996 ms
137,276 KB
testcase_04 AC 857 ms
83,548 KB
testcase_05 AC 1,561 ms
136,184 KB
testcase_06 AC 558 ms
66,596 KB
testcase_07 AC 115 ms
20,784 KB
testcase_08 AC 188 ms
23,700 KB
testcase_09 AC 68 ms
14,216 KB
testcase_10 AC 276 ms
34,984 KB
testcase_11 AC 966 ms
84,980 KB
testcase_12 AC 573 ms
65,864 KB
testcase_13 AC 280 ms
36,196 KB
testcase_14 AC 48 ms
11,508 KB
testcase_15 AC 430 ms
44,208 KB
testcase_16 AC 1,113 ms
103,136 KB
testcase_17 AC 1,139 ms
125,744 KB
testcase_18 AC 270 ms
34,804 KB
testcase_19 AC 980 ms
85,980 KB
testcase_20 AC 83 ms
16,392 KB
testcase_21 AC 167 ms
22,368 KB
testcase_22 AC 796 ms
74,908 KB
testcase_23 AC 460 ms
47,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, math, sets

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


proc solve() =
    let N = stdin.readLine.parseInt
    graph = newSeqWith(N, newSeq[int]())
    r = initHashSet[int]()
    for i in 0..<N:
        r.incl(i)
    var a, b: int
    for _ in 0..<N - 1:
        (a, b) = stdin.readLine.split.map(parseInt)
        a -= 1; b -= 1
        graph[a].add(b)
        r.excl(b)
    
    proc dfs(cur: int, parent: int): int =
        var dp_cum = 0
        if parent != -1:
            depth[cur] = depth[parent] + 1
        for child in graph[cur]:
            dp_cum += dfs(child, cur)
        dp[cur] = dp_cum + 1
        return dp[cur]
    
    let
        root = r.pop()
        _ = dfs(root, -1)
    
    var ans = 0
    for parent in 0..<N:
        for child in graph[parent]:
            ans += (depth[parent] + 1) * dp[child]
            ans = ans mod MOD
    echo ans


when is_main_module:
    solve()
0