結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  convexineq | 
| 提出日時 | 2021-05-10 05:36:58 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,437 ms / 3,000 ms | 
| コード長 | 655 bytes | 
| コンパイル時間 | 142 ms | 
| コンパイル使用メモリ | 82,076 KB | 
| 実行使用メモリ | 268,808 KB | 
| 最終ジャッジ日時 | 2024-09-19 09:31:08 | 
| 合計ジャッジ時間 | 13,147 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
import sys
n = int(input())
g = [[] for _ in range(n)]
is_child = [0]*n
for i in range(n-1):
    a,b = map(int,sys.stdin.readline().split())
    g[a-1].append(b-1)
    is_child[b-1] = 1
root = is_child.index(0)
order = []
st = [root]
parent = [-1]*n
depth = [1]*n
while st:
    v = st.pop()
    order.append(v)
    for c in g[v]:
        if c != parent[v]:
            st.append(c)
            parent[c] = v
            depth[c] = depth[v] + 1
size = [1]*n
for i in order[::-1]:
    if i==root: break
    size[parent[i]] += size[i]
MOD = 10**9+7
ans = 0
for i in range(n):
    if i==root: continue
    ans += size[i]*depth[parent[i]]%MOD
print(ans%MOD)
            
            
            
        