結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
7,912 KB
testcase_01 AC 14 ms
7,840 KB
testcase_02 AC 15 ms
7,840 KB
testcase_03 AC 14 ms
7,824 KB
testcase_04 AC 13 ms
7,820 KB
testcase_05 AC 14 ms
7,904 KB
testcase_06 AC 14 ms
7,868 KB
testcase_07 AC 14 ms
7,820 KB
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 AC 419 ms
25,236 KB
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]:
            # [c,w,cw,ww,cww]
            res = dfs(v)
            cww += res[4] + c * res[3] + w * res[2] + cw * res[1] + ww * res[0]
            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]
    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