結果

問題 No.439 チワワのなる木
ユーザー 🍡yurahuna
提出日時 2016-04-29 12:01:37
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 1,028 bytes
コンパイル時間 260 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 40,348 KB
最終ジャッジ日時 2024-10-04 17:38:20
合計ジャッジ時間 6,430 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict as ddict

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(input())
s = input()
G = ddict(list)
for i in range(N - 1):
    a, b = map(int, 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