結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
52,224 KB
testcase_01 AC 29 ms
51,968 KB
testcase_02 AC 663 ms
200,064 KB
testcase_03 AC 652 ms
274,696 KB
testcase_04 AC 849 ms
129,120 KB
testcase_05 AC 1,447 ms
165,376 KB
testcase_06 AC 499 ms
111,008 KB
testcase_07 AC 139 ms
84,096 KB
testcase_08 AC 193 ms
88,960 KB
testcase_09 AC 110 ms
81,264 KB
testcase_10 AC 256 ms
93,184 KB
testcase_11 AC 863 ms
133,884 KB
testcase_12 AC 469 ms
111,104 KB
testcase_13 AC 256 ms
94,336 KB
testcase_14 AC 112 ms
80,156 KB
testcase_15 AC 368 ms
103,296 KB
testcase_16 AC 1,008 ms
140,956 KB
testcase_17 AC 1,050 ms
143,744 KB
testcase_18 AC 262 ms
93,220 KB
testcase_19 AC 913 ms
135,296 KB
testcase_20 AC 125 ms
82,688 KB
testcase_21 AC 175 ms
87,040 KB
testcase_22 AC 744 ms
124,416 KB
testcase_23 AC 424 ms
105,728 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*(depth+1)//2)%mod
    ans%=mod
    if visited[now]==0:
        visited[now]=1
        for e in es[now]:
            stack.append((e, depth+1))
print(ans)
0