結果

問題 No.439 チワワのなる木
ユーザー h_noson
提出日時 2017-02-03 22:42:46
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 806 ms / 5,000 ms
コード長 791 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 51,200 KB
最終ジャッジ日時 2024-12-24 04:26:29
合計ジャッジ時間 7,396 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

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

sys.setrecursionlimit(150000)
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