結果

問題 No.439 チワワのなる木
ユーザー okadukiokaduki
提出日時 2017-12-09 01:05:14
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 755 ms / 5,000 ms
コード長 726 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 53,076 KB
最終ジャッジ日時 2023-08-19 23:06:16
合計ジャッジ時間 6,517 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,808 KB
testcase_01 AC 16 ms
7,736 KB
testcase_02 AC 16 ms
7,820 KB
testcase_03 AC 15 ms
7,852 KB
testcase_04 AC 15 ms
7,808 KB
testcase_05 AC 15 ms
7,800 KB
testcase_06 AC 15 ms
7,796 KB
testcase_07 AC 16 ms
7,916 KB
testcase_08 AC 16 ms
7,804 KB
testcase_09 AC 16 ms
7,804 KB
testcase_10 AC 16 ms
7,804 KB
testcase_11 AC 16 ms
7,804 KB
testcase_12 AC 16 ms
7,732 KB
testcase_13 AC 17 ms
8,228 KB
testcase_14 AC 18 ms
8,392 KB
testcase_15 AC 21 ms
8,604 KB
testcase_16 AC 25 ms
8,508 KB
testcase_17 AC 23 ms
8,432 KB
testcase_18 AC 425 ms
26,352 KB
testcase_19 AC 388 ms
25,508 KB
testcase_20 AC 638 ms
32,160 KB
testcase_21 AC 168 ms
15,852 KB
testcase_22 AC 145 ms
14,708 KB
testcase_23 AC 664 ms
35,460 KB
testcase_24 AC 755 ms
49,856 KB
testcase_25 AC 594 ms
33,076 KB
testcase_26 AC 572 ms
31,536 KB
testcase_27 AC 523 ms
53,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)

n = int(input())
s = input()
G = [[] for _ in range(n)]
for m in range(n-1):
    x, y = [int(i) for i in input().split()]
    x -= 1
    y -= 1
    G[x].append(y)
    G[y].append(x)

nc = s.count('c')
nw = s.count('w')

dp1 = [[0,0] for _ in range(n)]
def dfs1(u, p):
    dp1[u][0] = 1 if s[u] == 'c' else 0
    dp1[u][1] = 1 if s[u] == 'w' else 0

    res = 0
    for to in G[u]:
        if to == p: continue
        res += dfs1(to, u)
        dp1[u][0] += dp1[to][0]
        dp1[u][1] += dp1[to][1]
        if s[u] == 'w':
            res += dp1[to][0] * (nw - dp1[to][1] - 1)
    if s[u] == 'w':
        res += (nc - dp1[u][0]) * (dp1[u][1] - 1)
    return res

print(dfs1(0,-1))
0