結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-07-17 16:11:31 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                MLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 586 bytes | 
| コンパイル時間 | 205 ms | 
| コンパイル使用メモリ | 82,244 KB | 
| 実行使用メモリ | 849,568 KB | 
| 最終ジャッジ日時 | 2024-07-07 03:58:06 | 
| 合計ジャッジ時間 | 4,228 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | MLE * 1 -- * 21 | 
ソースコード
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
n = int(input())
root = set(range(n))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)
    root.remove(b - 1)
for s in root:
    break
dp = [0] * n
cnt = [0] * n
def dfs(pos, bpos):
    cnt[pos] = 1
    for npos in edges[pos]:
        if npos == bpos:
            continue
        dfs(npos, pos)
        dp[pos] += dp[npos] + cnt[npos]
        cnt[pos] += cnt[npos]
        dp[pos] %= MOD
        cnt[pos] %= MOD
    
dfs(s, -1)
print(sum(dp) % MOD)
            
            
            
        