結果

問題 No.1103 Directed Length Sum
ユーザー uni_pythonuni_python
提出日時 2020-07-06 21:53:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,176 ms / 3,000 ms
コード長 963 bytes
コンパイル時間 546 ms
コンパイル使用メモリ 81,844 KB
実行使用メモリ 259,208 KB
最終ジャッジ日時 2023-10-24 23:26:34
合計ジャッジ時間 23,627 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
67,892 KB
testcase_01 AC 63 ms
67,892 KB
testcase_02 AC 1,107 ms
251,572 KB
testcase_03 AC 1,006 ms
259,208 KB
testcase_04 AC 1,309 ms
162,188 KB
testcase_05 AC 2,176 ms
240,292 KB
testcase_06 AC 876 ms
132,540 KB
testcase_07 AC 278 ms
93,020 KB
testcase_08 AC 398 ms
99,856 KB
testcase_09 AC 213 ms
87,296 KB
testcase_10 AC 507 ms
107,160 KB
testcase_11 AC 1,418 ms
172,916 KB
testcase_12 AC 891 ms
132,696 KB
testcase_13 AC 510 ms
107,008 KB
testcase_14 AC 201 ms
84,192 KB
testcase_15 AC 716 ms
120,272 KB
testcase_16 AC 1,677 ms
184,084 KB
testcase_17 AC 1,717 ms
188,724 KB
testcase_18 AC 515 ms
107,028 KB
testcase_19 AC 1,471 ms
173,536 KB
testcase_20 AC 246 ms
89,920 KB
testcase_21 AC 348 ms
98,068 KB
testcase_22 AC 1,198 ms
151,532 KB
testcase_23 AC 785 ms
122,760 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=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()
    LL=[]#帰り順
    
    root=0
    for i in range(N):
        if P[i]==-1:
            root=i
        
    q.put(root)
    # LL.append(root)

        
    D=[0]*N
    while not q.empty():
        v=q.get()
        for nv in to[v]:
            D[nv]=D[v]+1
            q.put(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