結果

問題 No.439 チワワのなる木
ユーザー okadukiokaduki
提出日時 2017-12-09 01:03:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 725 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 10,892 KB
実行使用メモリ 62,156 KB
最終ジャッジ日時 2023-08-19 23:06:09
合計ジャッジ時間 6,375 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
7,792 KB
testcase_01 AC 14 ms
7,820 KB
testcase_02 AC 14 ms
7,740 KB
testcase_03 AC 14 ms
7,824 KB
testcase_04 AC 14 ms
7,788 KB
testcase_05 AC 15 ms
7,740 KB
testcase_06 AC 15 ms
7,792 KB
testcase_07 AC 14 ms
7,856 KB
testcase_08 AC 14 ms
7,816 KB
testcase_09 AC 15 ms
7,816 KB
testcase_10 AC 15 ms
7,816 KB
testcase_11 AC 15 ms
7,812 KB
testcase_12 AC 15 ms
7,780 KB
testcase_13 AC 17 ms
8,284 KB
testcase_14 AC 16 ms
8,160 KB
testcase_15 AC 19 ms
8,476 KB
testcase_16 AC 23 ms
8,572 KB
testcase_17 AC 21 ms
8,500 KB
testcase_18 AC 377 ms
26,380 KB
testcase_19 AC 356 ms
25,596 KB
testcase_20 AC 540 ms
32,320 KB
testcase_21 AC 156 ms
15,780 KB
testcase_22 AC 135 ms
14,712 KB
testcase_23 AC 559 ms
35,448 KB
testcase_24 AC 604 ms
49,776 KB
testcase_25 AC 511 ms
32,988 KB
testcase_26 AC 510 ms
31,520 KB
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(100000)

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