結果

問題 No.439 チワワのなる木
ユーザー okadukiokaduki
提出日時 2017-12-09 01:07:25
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 593 ms / 5,000 ms
コード長 769 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 60,164 KB
最終ジャッジ日時 2023-08-19 23:07:37
合計ジャッジ時間 5,583 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,800 KB
testcase_01 AC 16 ms
7,856 KB
testcase_02 AC 16 ms
7,908 KB
testcase_03 AC 15 ms
7,808 KB
testcase_04 AC 16 ms
7,848 KB
testcase_05 AC 15 ms
7,844 KB
testcase_06 AC 15 ms
7,784 KB
testcase_07 AC 16 ms
7,868 KB
testcase_08 AC 15 ms
7,816 KB
testcase_09 AC 16 ms
7,812 KB
testcase_10 AC 16 ms
7,784 KB
testcase_11 AC 16 ms
7,832 KB
testcase_12 AC 16 ms
7,812 KB
testcase_13 AC 16 ms
8,280 KB
testcase_14 AC 17 ms
8,284 KB
testcase_15 AC 19 ms
8,552 KB
testcase_16 AC 22 ms
8,884 KB
testcase_17 AC 20 ms
8,644 KB
testcase_18 AC 333 ms
31,268 KB
testcase_19 AC 311 ms
30,240 KB
testcase_20 AC 496 ms
38,548 KB
testcase_21 AC 124 ms
17,636 KB
testcase_22 AC 108 ms
16,320 KB
testcase_23 AC 518 ms
42,360 KB
testcase_24 AC 593 ms
56,828 KB
testcase_25 AC 453 ms
40,268 KB
testcase_26 AC 420 ms
38,608 KB
testcase_27 AC 373 ms
60,164 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