結果

問題 No.1103 Directed Length Sum
ユーザー convexineqconvexineq
提出日時 2021-05-10 05:36:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,411 ms / 3,000 ms
コード長 655 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 268,464 KB
最終ジャッジ日時 2023-10-19 13:22:47
合計ジャッジ時間 13,853 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,480 KB
testcase_01 AC 34 ms
53,480 KB
testcase_02 AC 564 ms
259,544 KB
testcase_03 AC 455 ms
268,464 KB
testcase_04 AC 817 ms
165,056 KB
testcase_05 AC 1,411 ms
239,076 KB
testcase_06 AC 511 ms
143,508 KB
testcase_07 AC 138 ms
91,028 KB
testcase_08 AC 190 ms
100,252 KB
testcase_09 AC 101 ms
84,296 KB
testcase_10 AC 258 ms
108,836 KB
testcase_11 AC 878 ms
182,448 KB
testcase_12 AC 520 ms
143,892 KB
testcase_13 AC 281 ms
110,784 KB
testcase_14 AC 93 ms
82,272 KB
testcase_15 AC 430 ms
122,080 KB
testcase_16 AC 938 ms
181,892 KB
testcase_17 AC 997 ms
212,024 KB
testcase_18 AC 243 ms
108,728 KB
testcase_19 AC 901 ms
195,656 KB
testcase_20 AC 113 ms
87,504 KB
testcase_21 AC 172 ms
98,060 KB
testcase_22 AC 722 ms
152,608 KB
testcase_23 AC 443 ms
134,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
n = int(input())
g = [[] for _ in range(n)]
is_child = [0]*n
for i in range(n-1):
    a,b = map(int,sys.stdin.readline().split())
    g[a-1].append(b-1)
    is_child[b-1] = 1
root = is_child.index(0)

order = []
st = [root]
parent = [-1]*n
depth = [1]*n
while st:
    v = st.pop()
    order.append(v)
    for c in g[v]:
        if c != parent[v]:
            st.append(c)
            parent[c] = v
            depth[c] = depth[v] + 1

size = [1]*n
for i in order[::-1]:
    if i==root: break
    size[parent[i]] += size[i]

MOD = 10**9+7
ans = 0
for i in range(n):
    if i==root: continue
    ans += size[i]*depth[parent[i]]%MOD
print(ans%MOD)
0