結果

問題 No.439 チワワのなる木
ユーザー h_nosonh_noson
提出日時 2017-02-03 22:36:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 778 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,796 KB
実行使用メモリ 26,140 KB
最終ジャッジ日時 2023-08-25 18:30:20
合計ジャッジ時間 6,415 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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
    print(u+1,c,w,cw,ww,cww)
    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