結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,848 KB
testcase_01 AC 15 ms
7,804 KB
testcase_02 AC 15 ms
7,840 KB
testcase_03 AC 15 ms
7,812 KB
testcase_04 AC 15 ms
7,760 KB
testcase_05 AC 15 ms
7,852 KB
testcase_06 AC 15 ms
7,776 KB
testcase_07 AC 15 ms
7,812 KB
testcase_08 AC 15 ms
7,900 KB
testcase_09 AC 15 ms
7,724 KB
testcase_10 AC 15 ms
7,844 KB
testcase_11 AC 15 ms
7,748 KB
testcase_12 AC 15 ms
7,800 KB
testcase_13 AC 16 ms
7,844 KB
testcase_14 AC 17 ms
8,316 KB
testcase_15 AC 20 ms
8,456 KB
testcase_16 AC 22 ms
8,420 KB
testcase_17 AC 21 ms
8,516 KB
testcase_18 AC 354 ms
20,688 KB
testcase_19 AC 330 ms
20,404 KB
testcase_20 AC 493 ms
24,872 KB
testcase_21 AC 143 ms
13,488 KB
testcase_22 AC 127 ms
12,820 KB
testcase_23 AC 527 ms
27,948 KB
testcase_24 AC 581 ms
45,340 KB
testcase_25 AC 468 ms
25,100 KB
testcase_26 AC 459 ms
23,768 KB
testcase_27 AC 470 ms
48,432 KB
権限があれば一括ダウンロードができます

ソースコード

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