結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-05-06 01:18:59
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,861 ms / 3,000 ms
コード長 695 bytes
コンパイル時間 332 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 263,544 KB
最終ジャッジ日時 2023-09-09 19:33:13
合計ジャッジ時間 18,392 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,416 KB
testcase_01 AC 69 ms
70,980 KB
testcase_02 AC 868 ms
260,940 KB
testcase_03 AC 729 ms
263,544 KB
testcase_04 AC 1,038 ms
175,120 KB
testcase_05 AC 1,861 ms
232,556 KB
testcase_06 AC 690 ms
146,164 KB
testcase_07 AC 220 ms
92,808 KB
testcase_08 AC 300 ms
102,648 KB
testcase_09 AC 172 ms
86,192 KB
testcase_10 AC 382 ms
110,712 KB
testcase_11 AC 1,145 ms
195,472 KB
testcase_12 AC 686 ms
146,556 KB
testcase_13 AC 381 ms
112,460 KB
testcase_14 AC 155 ms
84,324 KB
testcase_15 AC 549 ms
124,728 KB
testcase_16 AC 1,273 ms
187,588 KB
testcase_17 AC 1,320 ms
201,208 KB
testcase_18 AC 357 ms
110,432 KB
testcase_19 AC 1,157 ms
173,364 KB
testcase_20 AC 184 ms
89,228 KB
testcase_21 AC 265 ms
99,320 KB
testcase_22 AC 942 ms
169,300 KB
testcase_23 AC 569 ms
136,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#n = 頂点数,edge = 隣接リスト
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