結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:53:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,441 ms / 3,000 ms
コード長 474 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 274,880 KB
最終ジャッジ日時 2024-04-17 02:03:09
合計ジャッジ時間 14,204 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
52,096 KB
testcase_01 AC 33 ms
51,968 KB
testcase_02 AC 718 ms
200,640 KB
testcase_03 AC 701 ms
274,880 KB
testcase_04 AC 799 ms
128,916 KB
testcase_05 AC 1,441 ms
165,628 KB
testcase_06 AC 477 ms
111,232 KB
testcase_07 AC 139 ms
84,608 KB
testcase_08 AC 181 ms
89,028 KB
testcase_09 AC 104 ms
81,352 KB
testcase_10 AC 245 ms
93,372 KB
testcase_11 AC 907 ms
134,016 KB
testcase_12 AC 530 ms
111,232 KB
testcase_13 AC 265 ms
94,208 KB
testcase_14 AC 101 ms
80,640 KB
testcase_15 AC 398 ms
103,348 KB
testcase_16 AC 978 ms
140,828 KB
testcase_17 AC 1,105 ms
144,216 KB
testcase_18 AC 246 ms
93,236 KB
testcase_19 AC 925 ms
135,228 KB
testcase_20 AC 124 ms
82,816 KB
testcase_21 AC 185 ms
87,368 KB
testcase_22 AC 725 ms
124,672 KB
testcase_23 AC 442 ms
105,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
mod=10**9+7
es=[[] for _ in range(n)]
par=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    es[a-1].append(b-1)
    par[b-1]=1

for i in range(n):
    if par[i]==0:
        root=i
        break
stack=[(root,0)]
ans=0
visited=[0]*n
while stack:
    now,depth=stack.pop()
    ans+=(depth%mod)*((depth+1)%mod)//2
    ans%=mod
    if visited[now]==0:
        visited[now]=1
        for e in es[now]:
            stack.append((e, depth+1))
print(ans)
0