結果

問題 No.439 チワワのなる木
ユーザー h_nosonh_noson
提出日時 2017-02-03 22:41:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 791 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 10,836 KB
実行使用メモリ 57,256 KB
最終ジャッジ日時 2023-08-25 18:30:44
合計ジャッジ時間 5,949 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,748 KB
testcase_01 AC 16 ms
7,716 KB
testcase_02 AC 15 ms
7,752 KB
testcase_03 AC 15 ms
7,844 KB
testcase_04 AC 15 ms
7,904 KB
testcase_05 AC 15 ms
7,840 KB
testcase_06 AC 15 ms
7,804 KB
testcase_07 AC 15 ms
7,748 KB
testcase_08 AC 16 ms
7,808 KB
testcase_09 AC 16 ms
7,712 KB
testcase_10 AC 16 ms
7,840 KB
testcase_11 AC 15 ms
7,880 KB
testcase_12 AC 15 ms
7,776 KB
testcase_13 AC 17 ms
7,764 KB
testcase_14 AC 17 ms
8,316 KB
testcase_15 AC 20 ms
8,308 KB
testcase_16 AC 22 ms
8,540 KB
testcase_17 AC 22 ms
8,312 KB
testcase_18 AC 363 ms
20,752 KB
testcase_19 AC 333 ms
20,416 KB
testcase_20 AC 497 ms
24,740 KB
testcase_21 AC 146 ms
13,464 KB
testcase_22 AC 127 ms
12,792 KB
testcase_23 AC 534 ms
27,976 KB
testcase_24 AC 579 ms
45,136 KB
testcase_25 AC 462 ms
25,204 KB
testcase_26 AC 464 ms
23,736 KB
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

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(100000)
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