結果

問題 No.1103 Directed Length Sum
ユーザー uni_pythonuni_python
提出日時 2020-07-06 21:58:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,374 ms / 3,000 ms
コード長 903 bytes
コンパイル時間 1,721 ms
コンパイル使用メモリ 81,568 KB
実行使用メモリ 261,344 KB
最終ジャッジ日時 2023-10-24 23:27:06
合計ジャッジ時間 13,525 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,552 KB
testcase_01 AC 36 ms
53,552 KB
testcase_02 AC 571 ms
258,556 KB
testcase_03 AC 457 ms
261,344 KB
testcase_04 AC 812 ms
158,888 KB
testcase_05 AC 1,374 ms
214,552 KB
testcase_06 AC 515 ms
136,592 KB
testcase_07 AC 145 ms
90,316 KB
testcase_08 AC 201 ms
99,124 KB
testcase_09 AC 105 ms
83,888 KB
testcase_10 AC 280 ms
101,836 KB
testcase_11 AC 857 ms
171,436 KB
testcase_12 AC 517 ms
138,592 KB
testcase_13 AC 279 ms
103,916 KB
testcase_14 AC 96 ms
81,984 KB
testcase_15 AC 414 ms
127,036 KB
testcase_16 AC 980 ms
180,220 KB
testcase_17 AC 1,055 ms
176,724 KB
testcase_18 AC 269 ms
107,164 KB
testcase_19 AC 888 ms
171,552 KB
testcase_20 AC 121 ms
87,200 KB
testcase_21 AC 188 ms
97,032 KB
testcase_22 AC 723 ms
152,180 KB
testcase_23 AC 437 ms
131,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    to=[[]for _ in range(N)]
    P=[-1]*N
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        to[a].append(b)
        P[b]=a
    
    L=[]
    LL=[]#帰り順
    
    root=0
    for i in range(N):
        if P[i]==-1:
            root=i
        
    L.append(root)

        
    D=[0]*N
    while L:
        v=L.pop()
        for nv in to[v]:
            D[nv]=D[v]+1
            L.append(nv)
            LL.append(nv)
            
    ch=[0]*N#子を根とする木のサイズ
            
    for v in LL[::-1]:
        p=P[v]
        ch[p]+=ch[v]+1
                
    ans=0
    
    for i in range(N):
        ans=(ans+(D[i])*(ch[i]+1))%mod
    print(ans)
    


    


main()
0