結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-25 20:42:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 698 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 82,396 KB
実行使用メモリ 117,324 KB
最終ジャッジ日時 2024-04-15 04:28:03
合計ジャッジ時間 9,038 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,728 KB
testcase_01 AC 40 ms
53,888 KB
testcase_02 AC 44 ms
53,376 KB
testcase_03 WA -
testcase_04 AC 39 ms
54,144 KB
testcase_05 AC 41 ms
53,760 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 41 ms
53,888 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)



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

    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]

print(ans)
0