結果

問題 No.439 チワワのなる木
ユーザー okadukiokaduki
提出日時 2017-12-09 00:59:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 683 bytes
コンパイル時間 117 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 33,968 KB
最終ジャッジ日時 2023-08-19 22:46:26
合計ジャッジ時間 6,656 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,804 KB
testcase_01 AC 16 ms
7,876 KB
testcase_02 AC 16 ms
7,784 KB
testcase_03 AC 16 ms
7,732 KB
testcase_04 AC 16 ms
7,852 KB
testcase_05 AC 16 ms
7,916 KB
testcase_06 AC 16 ms
7,892 KB
testcase_07 AC 16 ms
7,816 KB
testcase_08 AC 16 ms
7,792 KB
testcase_09 AC 16 ms
7,856 KB
testcase_10 AC 16 ms
7,924 KB
testcase_11 AC 16 ms
7,868 KB
testcase_12 AC 15 ms
7,908 KB
testcase_13 AC 17 ms
8,328 KB
testcase_14 AC 17 ms
8,232 KB
testcase_15 AC 21 ms
8,460 KB
testcase_16 AC 24 ms
8,620 KB
testcase_17 AC 23 ms
8,588 KB
testcase_18 AC 436 ms
26,196 KB
testcase_19 AC 417 ms
25,604 KB
testcase_20 AC 618 ms
32,320 KB
testcase_21 AC 165 ms
15,876 KB
testcase_22 AC 146 ms
14,812 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 572 ms
33,116 KB
testcase_26 AC 559 ms
31,628 KB
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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