結果

問題 No.1103 Directed Length Sum
ユーザー stngstng
提出日時 2022-09-03 18:35:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 900 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 160,416 KB
最終ジャッジ日時 2024-04-28 15:57:29
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
17,824 KB
testcase_01 AC 26 ms
10,880 KB
testcase_02 TLE -
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
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
sys.setrecursionlimit(10**7)
from functools import lru_cache

mod = 10**9+7

n = int(input())
li = [0]*n
ab = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    ab[a].append(b)
    li[b] = 1

for i in range(n):
    if li[i] == 0:
        idx = i
        break

num = [0]*n

@lru_cache(maxsize = None)
def dfs(v):
    if num[v] != 0:
        return num[v]
    for i in range(len(ab[v])):
        num[v] += dfs(ab[v][i])
    num[v] += 1
    return num[v]

dfs(idx)
dp = [-1]*n

@lru_cache(maxsize = None)
def dfs2(v):
    if dp[v] != -1:
        return dp[v]
    for i in range(len(ab[v])):
        dp[v] += dfs2(ab[v][i])+num[ab[v][i]]
        dp[v] %= mod
    dp[v] += 1
    dp[v] %= mod
    return dp[v]
    
#print(dp,num)
dfs2(idx)
ans = 0
for i in range(n):
    ans += dp[i]
    ans %= mod
#print(sum(dp)%mod)
print(ans)
#print(dp)
0