結果

問題 No.439 チワワのなる木
ユーザー okadukiokaduki
提出日時 2017-12-09 01:11:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 661 ms / 5,000 ms
コード長 769 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 86,828 KB
実行使用メモリ 242,984 KB
最終ジャッジ日時 2023-08-19 23:08:52
合計ジャッジ時間 6,889 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,196 KB
testcase_01 AC 67 ms
71,192 KB
testcase_02 AC 67 ms
71,552 KB
testcase_03 AC 67 ms
71,504 KB
testcase_04 AC 69 ms
71,228 KB
testcase_05 AC 67 ms
71,188 KB
testcase_06 AC 68 ms
71,316 KB
testcase_07 AC 67 ms
71,388 KB
testcase_08 AC 67 ms
71,316 KB
testcase_09 AC 68 ms
71,176 KB
testcase_10 AC 68 ms
71,380 KB
testcase_11 AC 67 ms
71,196 KB
testcase_12 AC 68 ms
71,192 KB
testcase_13 AC 68 ms
71,284 KB
testcase_14 AC 69 ms
71,056 KB
testcase_15 AC 87 ms
76,560 KB
testcase_16 AC 96 ms
79,448 KB
testcase_17 AC 92 ms
77,196 KB
testcase_18 AC 341 ms
100,456 KB
testcase_19 AC 247 ms
97,256 KB
testcase_20 AC 389 ms
106,500 KB
testcase_21 AC 176 ms
86,680 KB
testcase_22 AC 229 ms
87,884 KB
testcase_23 AC 661 ms
242,984 KB
testcase_24 AC 518 ms
183,284 KB
testcase_25 AC 215 ms
104,644 KB
testcase_26 AC 217 ms
104,284 KB
testcase_27 AC 413 ms
197,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)

lines = sys.stdin.read().splitlines()
n = int(lines[0])
s = lines[1]
G = [[] for _ in range(n)]
for m in range(n-1):
    x, y = [int(i) for i in lines[2+m].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