結果

問題 No.439 チワワのなる木
ユーザー mayoko_mayoko_
提出日時 2016-04-29 13:06:05
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,081 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 73,036 KB
最終ジャッジ日時 2024-04-15 05:22:58
合計ジャッジ時間 3,042 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def dfs(v, par):
    ret = [0, 0]    # (ww, w)
    for ch in G[v]:
        if ch == par:
            continue
        child = dfs(ch, v)
        ret[0] += child[0]
        ret[1] += child[1]

    if s[v] == 'w':
        ret[0] += ret[1]
        ret[1] += 1

    C[v] = ret
    return ret

ans = 0
def dfs2(v, par, p):
    global ans
    if s[v] == 'c':
        ans += p[0]
        for ch in G[v]:
            if ch == par:
                continue
            ans += C[ch][0]

    for ch in G[v]:
        if ch == par:
            continue
        tmp = list(p)
        tmp[0] += C[v][0]; tmp[1] += C[v][1]
        tmp[0] -= C[ch][0]; tmp[1] -= C[ch][1]
        if s[v] == 'w':
            tmp[0] -= C[ch][1]
            tmp[0] += p[1]
        dfs2(ch, v, tmp)

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

C = [None] * N
dfs(0, -1)
dfs2(0, -1, [0, 0])
print(ans)
0