結果

問題 No.439 チワワのなる木
ユーザー H3PO4H3PO4
提出日時 2021-07-10 09:05:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 972 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 35,344 KB
最終ジャッジ日時 2023-09-14 18:41:08
合計ジャッジ時間 6,816 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,452 KB
testcase_01 AC 19 ms
8,512 KB
testcase_02 AC 20 ms
8,512 KB
testcase_03 AC 19 ms
8,588 KB
testcase_04 AC 19 ms
8,640 KB
testcase_05 AC 19 ms
8,612 KB
testcase_06 AC 18 ms
8,508 KB
testcase_07 AC 19 ms
8,448 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 560 ms
31,720 KB
testcase_25 AC 551 ms
28,928 KB
testcase_26 WA -
testcase_27 AC 380 ms
35,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.readline

N = int(input())
S = input()
T = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = (int(x) - 1 for x in input().split())
    T[a].append(b)
    T[b].append(a)

# dfs
dfs_order = []
parent = [None] * N
d = deque([0])
nonvisited = [True] * N
nonvisited[0] = False
while d:
    v = d.pop()
    dfs_order.append(v)
    for x in T[v]:
        if nonvisited[x]:
            d.append(x)
            parent[x] = v
            nonvisited[x] = False

Csum, Wsum = S.count('c'), S.count('w')

# 子から親へ
C, W = [0] * N, [0] * N
ans = 0
for v in reversed(dfs_order):
    for child in T[v]:
        if child == parent[v]:
            continue
        C[v] += C[child]
        W[v] += W[child]
        if S[child] == 'c':
            C[v] += 1
        else:  # S[child] == 'w'
            W[v] += 1
    if S[v] == 'w':  # c[w]w
        ans += C[v] * (Wsum - W[v] - 1) + W[v] * (Csum - C[v])
print(ans)
0