結果

問題 No.1103 Directed Length Sum
ユーザー uni_pythonuni_python
提出日時 2020-07-05 16:28:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,137 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 81,812 KB
実行使用メモリ 256,084 KB
最終ジャッジ日時 2023-10-23 20:20:16
合計ジャッジ時間 30,070 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
67,928 KB
testcase_01 AC 65 ms
67,928 KB
testcase_02 AC 1,611 ms
216,832 KB
testcase_03 AC 1,550 ms
256,084 KB
testcase_04 WA -
testcase_05 TLE -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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=10**6
    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
    
    import queue
    q=queue.Queue()
    q2=queue.Queue()
    root=0
    for i in range(N):
        if P[i]==-1:
            root=i
        if len(to[i])==0:
            q2.put(i)
        
    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

    used[root]=1#最後にp=-1を反映させたくないので
            
    while not q2.empty():
        v=q2.get()
        p=P[v]
        ch[p]+=ch[v]+1
        if used[p]==0:
            q2.put(p)
            used[p]=1
                
    ans=0
    
    for i in range(N):
        ans=(ans+(D[i])*(ch[i]+1))%mod
    print(ans)
    


    


main()
0