結果

問題 No.1103 Directed Length Sum
ユーザー uni_pythonuni_python
提出日時 2020-07-05 16:06:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,193 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 81,772 KB
実行使用メモリ 315,332 KB
最終ジャッジ日時 2023-10-23 19:46:17
合計ジャッジ時間 13,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
68,020 KB
testcase_01 AC 61 ms
68,020 KB
testcase_02 AC 2,044 ms
315,332 KB
testcase_03 AC 1,928 ms
309,380 KB
testcase_04 WA -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

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)]
    fro=[[]for _ in range(N)]
    for i in range(N-1):
        a,b=MI()
        a-=1
        b-=1
        # a,b=i,i+1
        to[a].append(b)
        fro[b].append(a)
        
    root=0
    for i in range(N):
        if len(fro[i])==0:
            root=i
            break
        
    import queue
    q=queue.Queue()
    q.put(root)
        
    D=[0]*N
    while not q.empty():
        v=q.get()
        for nv in to[v]:
            D[nv]=D[v]+1
            q.put(nv)
            
    ch=[0]*N#子を根とする木のサイズ
    used=[0]*N
    for i in range(N):
        if len(to[i])==0:
            q.put(i)
            used[i]=1
            
    while not q.empty():
        v=q.get()
        for nv in fro[v]:
            ch[nv]+=ch[v]+1
            if used[nv]==0:
                q.put(nv)
            used[nv]=1
                
    ans=0
    for i in range(N):
        temp=((D[i])*(ch[i]+1))%mod
        ans=(ans+temp)%mod
    print(ans)

    


main()
0