結果

問題 No.1103 Directed Length Sum
ユーザー ttrttr
提出日時 2020-07-03 22:31:44
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,402 ms / 3,000 ms
コード長 475 bytes
コンパイル時間 250 ms
コンパイル使用メモリ 81,680 KB
実行使用メモリ 213,988 KB
最終ジャッジ日時 2023-10-17 05:30:45
合計ジャッジ時間 13,345 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
53,296 KB
testcase_01 AC 31 ms
55,344 KB
testcase_02 AC 658 ms
191,684 KB
testcase_03 AC 643 ms
213,988 KB
testcase_04 AC 757 ms
124,600 KB
testcase_05 AC 1,402 ms
157,172 KB
testcase_06 AC 504 ms
107,980 KB
testcase_07 AC 149 ms
83,476 KB
testcase_08 AC 194 ms
87,976 KB
testcase_09 AC 121 ms
80,660 KB
testcase_10 AC 251 ms
92,176 KB
testcase_11 AC 833 ms
129,004 KB
testcase_12 AC 468 ms
108,304 KB
testcase_13 AC 241 ms
92,608 KB
testcase_14 AC 100 ms
79,860 KB
testcase_15 AC 361 ms
101,532 KB
testcase_16 AC 898 ms
134,924 KB
testcase_17 AC 982 ms
137,320 KB
testcase_18 AC 231 ms
92,072 KB
testcase_19 AC 851 ms
130,256 KB
testcase_20 AC 129 ms
82,124 KB
testcase_21 AC 182 ms
86,840 KB
testcase_22 AC 705 ms
120,392 KB
testcase_23 AC 406 ms
103,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())
E = [[] for _ in range(N)]
par = [-1]*N
for _ in range(N-1):
    a,b = map(int, input().split())
    E[a-1].append(b-1)
    par[b-1] = a-1

for i in range(N):
    if par[i] == -1:
        root = i
        break

ans = 0
mod = 10**9+7
q = deque()
q.append((root, 0))
while q:
    temp = q.pop()
    p = temp[0]
    rnk = temp[1]
    ans += rnk*(rnk+1)//2
    ans %= mod
    for v in E[p]:
        q.append((v, rnk+1))

print(ans)
0