結果

問題 No.1103 Directed Length Sum
ユーザー 👑 tamatotamato
提出日時 2020-07-03 21:51:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,085 ms / 3,000 ms
コード長 763 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,044 KB
実行使用メモリ 199,084 KB
最終ジャッジ日時 2023-10-17 01:18:05
合計ジャッジ時間 13,247 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,824 KB
testcase_01 AC 38 ms
55,824 KB
testcase_02 AC 418 ms
199,084 KB
testcase_03 AC 352 ms
196,172 KB
testcase_04 AC 630 ms
133,384 KB
testcase_05 AC 1,085 ms
174,688 KB
testcase_06 AC 426 ms
113,688 KB
testcase_07 AC 139 ms
84,416 KB
testcase_08 AC 191 ms
88,440 KB
testcase_09 AC 104 ms
80,868 KB
testcase_10 AC 242 ms
93,692 KB
testcase_11 AC 688 ms
137,404 KB
testcase_12 AC 433 ms
113,876 KB
testcase_13 AC 250 ms
94,032 KB
testcase_14 AC 100 ms
80,028 KB
testcase_15 AC 365 ms
105,624 KB
testcase_16 AC 805 ms
144,600 KB
testcase_17 AC 829 ms
148,276 KB
testcase_18 AC 246 ms
93,644 KB
testcase_19 AC 714 ms
139,664 KB
testcase_20 AC 116 ms
81,604 KB
testcase_21 AC 178 ms
87,500 KB
testcase_22 AC 601 ms
128,436 KB
testcase_23 AC 382 ms
105,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    def f(n):
        return (n * (n+1) // 2)%mod

    N = int(input())
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        par[b] = a
        child[a].append(b)

    for v in range(1, N+1):
        if par[v] == 0:
            v0 = v
            break
    depth = [-1] * (N+1)
    que = deque()
    que.append(v0)
    ans = 0
    while que:
        v = que.popleft()
        depth[v] = depth[par[v]] + 1
        for u in child[v]:
            que.append(u)
        ans = (ans + f(depth[v]))%mod
    print(ans)


if __name__ == '__main__':
    main()
0