結果

問題 No.439 チワワのなる木
ユーザー tktk_snsntktk_snsn
提出日時 2021-01-04 05:13:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 799 bytes
コンパイル時間 232 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 106,096 KB
最終ジャッジ日時 2024-04-22 02:07:25
合計ジャッジ時間 3,902 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,504 KB
testcase_01 AC 42 ms
53,932 KB
testcase_02 AC 40 ms
53,376 KB
testcase_03 AC 40 ms
54,016 KB
testcase_04 AC 41 ms
53,760 KB
testcase_05 AC 42 ms
54,016 KB
testcase_06 AC 42 ms
53,632 KB
testcase_07 AC 41 ms
54,016 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 171 ms
97,280 KB
testcase_25 AC 166 ms
102,016 KB
testcase_26 WA -
testcase_27 AC 133 ms
97,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import Counter
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
S = input()
cnt = Counter(S)
G = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

topo = []
par = [-1] * N
que = [0]
while que:
    s = que.pop()
    topo.append(s)
    for t in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        que.append(t)

ans = 0
C = [0] * N
W = [0] * N
for i in topo[::-1][:-1]:
    if S[i] == "c":
        C[i] += 1
    else:
        W[i] += 1

    p = par[i]
    if S[p] == "w":
        w = cnt["w"] - W[i] - 1
        ans += C[i] * w
        c = cnt["c"] - C[i]
        ans += c * W[i]

    C[p] += C[i]
    W[p] += W[i]

print(ans)
0