結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-25 20:43:58
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 778 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,784 KB
実行使用メモリ 120,368 KB
最終ジャッジ日時 2024-04-15 04:28:18
合計ジャッジ時間 8,894 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,216 KB
testcase_01 AC 39 ms
54,388 KB
testcase_02 AC 37 ms
54,724 KB
testcase_03 WA -
testcase_04 AC 36 ms
53,880 KB
testcase_05 AC 37 ms
54,024 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 37 ms
53,848 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict as ddict
import sys
sys.setrecursionlimit(100000)

mod = 1000000007

def dfs(now, pre):
    ret = [0, 0, 0]

    for nxt in G[now]:
        if nxt == pre:
            continue
        child = dfs(nxt, now)
        for i in range(3):
            ret[i] += child[i]

    if s[now] == 'c':
        ret[2] += ret[1]
    else:
        ret[1] += ret[0]
        ret[0] += 1

    for i in range(3):
        ret[i] %= mod

    return ret

#################################

N = int(input())
s = input()
G = ddict(list)

for i in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1; b -= 1
    G[a].append(b)
    G[b].append(a)

ans = 0
for i in range(N):
    if s[i] == 'c':
        ans += dfs(i, -1)[2]
        ans %= mod

print(ans)
0