結果

問題 No.1103 Directed Length Sum
ユーザー ttrttr
提出日時 2020-07-03 22:34:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,573 ms / 3,000 ms
コード長 470 bytes
コンパイル時間 1,106 ms
コンパイル使用メモリ 81,668 KB
実行使用メモリ 191,636 KB
最終ジャッジ日時 2023-10-17 05:35:31
合計ジャッジ時間 15,737 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,332 KB
testcase_01 AC 41 ms
55,380 KB
testcase_02 AC 753 ms
191,636 KB
testcase_03 AC 622 ms
190,712 KB
testcase_04 AC 907 ms
124,500 KB
testcase_05 AC 1,573 ms
157,096 KB
testcase_06 AC 589 ms
107,868 KB
testcase_07 AC 174 ms
83,384 KB
testcase_08 AC 242 ms
87,884 KB
testcase_09 AC 140 ms
80,572 KB
testcase_10 AC 323 ms
92,080 KB
testcase_11 AC 989 ms
128,916 KB
testcase_12 AC 595 ms
108,220 KB
testcase_13 AC 321 ms
92,520 KB
testcase_14 AC 129 ms
79,768 KB
testcase_15 AC 487 ms
101,484 KB
testcase_16 AC 1,110 ms
134,840 KB
testcase_17 AC 1,179 ms
137,228 KB
testcase_18 AC 322 ms
91,980 KB
testcase_19 AC 1,013 ms
130,176 KB
testcase_20 AC 150 ms
82,044 KB
testcase_21 AC 223 ms
86,752 KB
testcase_22 AC 814 ms
120,312 KB
testcase_23 AC 502 ms
103,112 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)
while q:
    temp = q.pop()
    p = temp%N
    rnk = temp//N
    ans += rnk*(rnk+1)//2
    ans %= mod
    for v in E[p]:
        q.append((rnk+1)*N+v)

print(ans)
0