結果

問題 No.1103 Directed Length Sum
ユーザー 37zigen37zigen
提出日時 2020-07-03 22:47:46
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 565 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 267,488 KB
最終ジャッジ日時 2023-10-17 05:50:20
合計ジャッジ時間 21,842 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,372 KB
testcase_01 AC 35 ms
53,372 KB
testcase_02 RE -
testcase_03 AC 902 ms
267,488 KB
testcase_04 AC 1,380 ms
151,076 KB
testcase_05 AC 2,457 ms
202,884 KB
testcase_06 AC 886 ms
125,568 KB
testcase_07 AC 224 ms
87,992 KB
testcase_08 AC 321 ms
94,076 KB
testcase_09 AC 165 ms
83,548 KB
testcase_10 AC 484 ms
101,116 KB
testcase_11 AC 1,521 ms
157,500 KB
testcase_12 AC 879 ms
125,676 KB
testcase_13 AC 423 ms
101,456 KB
testcase_14 AC 153 ms
82,340 KB
testcase_15 AC 669 ms
114,456 KB
testcase_16 AC 1,698 ms
167,652 KB
testcase_17 AC 1,815 ms
171,636 KB
testcase_18 AC 433 ms
100,572 KB
testcase_19 AC 1,559 ms
159,568 KB
testcase_20 AC 201 ms
85,244 KB
testcase_21 AC 303 ms
92,440 KB
testcase_22 AC 1,263 ms
144,244 KB
testcase_23 AC 739 ms
117,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5**6)


MOD=10**9+7
N=int(input())
g=[[] for _ in range(N+1)]
sz=[0]*(N+1)
root=-1
deg=[0]*(N+1)
for _ in range(N-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
    deg[b]+=1

for i in range(1,N+1):
    if deg[i]==0:
        root=i
ans=0

def dfs(cur,par,d):
    global ans
    sz[cur]=1
    for dst in g[cur]:
        if dst==par:
            continue
        dfs(dst,cur,d+1)
        sz[cur]+=sz[dst]
        ans+=sz[dst]*(d+1)%MOD
        if ans>=MOD:
            ans-=MOD

dfs(root,-1,0)
print(ans)

0