結果

問題 No.1103 Directed Length Sum
ユーザー marroncastlemarroncastle
提出日時 2020-07-25 19:31:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,432 ms / 3,000 ms
コード長 605 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 271,204 KB
最終ジャッジ日時 2023-09-09 21:09:06
合計ジャッジ時間 15,124 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,552 KB
testcase_01 AC 92 ms
71,800 KB
testcase_02 AC 597 ms
196,012 KB
testcase_03 AC 697 ms
271,204 KB
testcase_04 AC 802 ms
145,252 KB
testcase_05 AC 1,432 ms
211,668 KB
testcase_06 AC 538 ms
117,656 KB
testcase_07 AC 176 ms
85,284 KB
testcase_08 AC 234 ms
90,584 KB
testcase_09 AC 148 ms
82,092 KB
testcase_10 AC 291 ms
95,608 KB
testcase_11 AC 883 ms
151,272 KB
testcase_12 AC 568 ms
116,840 KB
testcase_13 AC 293 ms
96,104 KB
testcase_14 AC 142 ms
81,020 KB
testcase_15 AC 426 ms
108,204 KB
testcase_16 AC 1,004 ms
163,320 KB
testcase_17 AC 1,034 ms
167,804 KB
testcase_18 AC 287 ms
95,864 KB
testcase_19 AC 919 ms
155,736 KB
testcase_20 AC 159 ms
84,680 KB
testcase_21 AC 209 ms
88,948 KB
testcase_22 AC 728 ms
137,460 KB
testcase_23 AC 445 ms
111,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    from collections import deque
    import sys
    input = sys.stdin.readline
    N = int(input())
    edge = [[] for _ in range(N)]
    roots = [True]*N
    for _ in range(N-1):
        a,b = map(int, input().split())
        roots[b-1] = False
        edge[a-1].append(b-1)

    ans = 0
    mod = 10**9+7
    d = deque()
    root = roots.index(True)
    d.append([root,0])
    while len(d)>0:
        v,depth = d.popleft()
        ans += depth*(depth+1)//2
        ans %= mod
        for w in edge[v]:
            d.append([w,depth+1])
    print(ans)

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