結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-25 20:42:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 698 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 31,796 KB
最終ジャッジ日時 2024-04-15 04:27:41
合計ジャッジ時間 10,767 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,880 KB
testcase_01 AC 29 ms
10,752 KB
testcase_02 AC 29 ms
10,752 KB
testcase_03 WA -
testcase_04 AC 29 ms
11,008 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 30 ms
10,752 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