結果

問題 No.439 チワワのなる木
ユーザー h_noson
提出日時 2017-02-03 22:10:08
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 772 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 28,928 KB
最終ジャッジ日時 2024-12-24 04:23:47
合計ジャッジ時間 5,607 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 8 WA * 17 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(u):
    used[u] = True
    c = w = cw = ww = cww = 0
    for v in e[u]:
        if not used[v]:
            # [c,w,cw,ww,cww]
            res = dfs(v)
            if s[u] == 'c':
                cww += res[3]
            else:
                cw += res[0]
                ww += res[1]
                cww += res[2] + c * res[1] + w * res[0]
            c += res[0]
            w += res[1]
            cw += res[2]
            ww += res[3]
            cww += res[4]
    if s[u] == 'c':
        c += 1
    else:
        w += 1
    return [c,w,cw,ww,cww]

n = int(input())
s = input()
e = [[] for i in range(n)]
used = [False] * n
for i in range(n-1):
    a, b = map(int,input().split(' '))
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
print(dfs(0)[4])
0