結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-05-06 01:20:52
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,587 ms / 3,000 ms
コード長 693 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 268,352 KB
最終ジャッジ日時 2023-09-09 20:03:56
合計ジャッジ時間 16,514 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,952 KB
testcase_01 AC 73 ms
71,092 KB
testcase_02 AC 622 ms
256,172 KB
testcase_03 AC 515 ms
268,352 KB
testcase_04 AC 887 ms
172,672 KB
testcase_05 AC 1,587 ms
247,796 KB
testcase_06 AC 579 ms
144,828 KB
testcase_07 AC 188 ms
92,312 KB
testcase_08 AC 244 ms
101,748 KB
testcase_09 AC 140 ms
85,616 KB
testcase_10 AC 322 ms
110,028 KB
testcase_11 AC 964 ms
190,684 KB
testcase_12 AC 569 ms
144,996 KB
testcase_13 AC 324 ms
111,928 KB
testcase_14 AC 130 ms
83,500 KB
testcase_15 AC 469 ms
122,704 KB
testcase_16 AC 1,088 ms
183,208 KB
testcase_17 AC 1,151 ms
212,900 KB
testcase_18 AC 310 ms
109,820 KB
testcase_19 AC 995 ms
196,780 KB
testcase_20 AC 158 ms
89,072 KB
testcase_21 AC 231 ms
99,260 KB
testcase_22 AC 834 ms
158,776 KB
testcase_23 AC 504 ms
135,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
n=int(input())
edge=[[] for i in range(n)]
isroot=[True]*n
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1;b-=1
    edge[a].append(b)
    isroot[b]=False
Root=0
for i in range(n):
    if isroot[i]:Root=i;break   
order = []
par = [-1]*n
tank = [Root]
depth=[0]*n
while tank:
    pele = tank.pop()
    order.append(pele)
    for e in edge[pele]:
        if e == par[pele]:
            continue
        tank.append(e)
        par[e] = pele
        depth[e]=depth[pele]+1
size = [1]*n
for e in order[::-1]:
    if e == Root:
        break
    size[par[e]] += size[e]
ans=0
for i in range(n):
    ans+=size[i]*depth[i]
    ans%=10**9+7
print(ans)
0