結果

問題 No.1103 Directed Length Sum
ユーザー Ldio03Ldio03
提出日時 2020-06-28 18:45:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,401 ms / 3,000 ms
コード長 693 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 268,920 KB
最終ジャッジ日時 2023-09-22 07:38:16
合計ジャッジ時間 15,423 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,184 KB
testcase_01 AC 71 ms
71,392 KB
testcase_02 AC 615 ms
260,536 KB
testcase_03 AC 489 ms
268,920 KB
testcase_04 AC 772 ms
172,692 KB
testcase_05 AC 1,401 ms
247,996 KB
testcase_06 AC 493 ms
144,824 KB
testcase_07 AC 160 ms
92,620 KB
testcase_08 AC 204 ms
101,736 KB
testcase_09 AC 130 ms
85,736 KB
testcase_10 AC 258 ms
110,240 KB
testcase_11 AC 846 ms
190,808 KB
testcase_12 AC 496 ms
145,228 KB
testcase_13 AC 275 ms
112,092 KB
testcase_14 AC 119 ms
83,664 KB
testcase_15 AC 394 ms
123,240 KB
testcase_16 AC 957 ms
183,628 KB
testcase_17 AC 1,026 ms
213,100 KB
testcase_18 AC 259 ms
110,348 KB
testcase_19 AC 883 ms
197,104 KB
testcase_20 AC 145 ms
89,284 KB
testcase_21 AC 198 ms
99,568 KB
testcase_22 AC 694 ms
159,016 KB
testcase_23 AC 415 ms
135,428 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