結果

問題 No.1103 Directed Length Sum
ユーザー brthyyjpbrthyyjp
提出日時 2020-09-09 02:52:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,338 ms / 3,000 ms
コード長 946 bytes
コンパイル時間 2,659 ms
コンパイル使用メモリ 83,280 KB
実行使用メモリ 383,276 KB
最終ジャッジ日時 2023-08-20 16:51:22
合計ジャッジ時間 19,749 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,336 KB
testcase_01 AC 73 ms
70,752 KB
testcase_02 AC 818 ms
380,836 KB
testcase_03 AC 790 ms
383,276 KB
testcase_04 AC 1,102 ms
223,812 KB
testcase_05 AC 2,338 ms
307,756 KB
testcase_06 AC 712 ms
163,496 KB
testcase_07 AC 203 ms
99,792 KB
testcase_08 AC 281 ms
108,532 KB
testcase_09 AC 142 ms
87,744 KB
testcase_10 AC 384 ms
119,708 KB
testcase_11 AC 1,216 ms
234,880 KB
testcase_12 AC 734 ms
164,640 KB
testcase_13 AC 385 ms
126,012 KB
testcase_14 AC 134 ms
85,684 KB
testcase_15 AC 567 ms
151,828 KB
testcase_16 AC 1,331 ms
246,304 KB
testcase_17 AC 1,435 ms
255,016 KB
testcase_18 AC 347 ms
121,368 KB
testcase_19 AC 1,268 ms
236,240 KB
testcase_20 AC 158 ms
89,920 KB
testcase_21 AC 266 ms
108,572 KB
testcase_22 AC 973 ms
209,752 KB
testcase_23 AC 605 ms
152,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = sys.stdin.buffer.readline
#input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

mod = 10**9+7

def main():
    n = int(input())
    g = [[] for _ in range(n)]
    ind = [[] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        a, b = a-1, b-1
        g[a].append(b)
        ind[b].append(a)

    root = -1
    for i in range(n):
        if len(ind[i]) == 0:
            root = i
            break

    s = []
    s.append(root)
    order = []
    par = [-1]*n
    depth = [0]*n
    while s:
        v = s.pop()
        order.append(v)
        for u in g[v]:
            if u == par[v]:
                continue
            par[u] = v
            s.append(u)
            depth[u] = depth[v]+1

    #print(depth)
    ans = 0
    for i in range(n):
        d = depth[i]
        ans += d*(d+1)//2
        ans %= mod
    print(ans)

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