結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,776 KB
testcase_01 AC 16 ms
7,832 KB
testcase_02 AC 15 ms
7,840 KB
testcase_03 AC 16 ms
7,808 KB
testcase_04 AC 16 ms
7,812 KB
testcase_05 AC 16 ms
7,788 KB
testcase_06 AC 16 ms
7,724 KB
testcase_07 AC 16 ms
7,724 KB
testcase_08 AC 16 ms
7,904 KB
testcase_09 AC 16 ms
7,812 KB
testcase_10 AC 16 ms
7,840 KB
testcase_11 AC 16 ms
7,780 KB
testcase_12 AC 16 ms
7,756 KB
testcase_13 AC 17 ms
7,876 KB
testcase_14 AC 18 ms
7,856 KB
testcase_15 AC 21 ms
8,348 KB
testcase_16 AC 24 ms
8,560 KB
testcase_17 AC 21 ms
8,404 KB
testcase_18 AC 392 ms
20,704 KB
testcase_19 AC 374 ms
20,388 KB
testcase_20 AC 531 ms
24,672 KB
testcase_21 AC 152 ms
13,480 KB
testcase_22 AC 132 ms
12,716 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 508 ms
25,276 KB
testcase_26 AC 500 ms
23,616 KB
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
    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