結果

問題 No.439 チワワのなる木
ユーザー ckawatakckawatak
提出日時 2018-03-29 00:17:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 656 ms / 5,000 ms
コード長 884 bytes
コンパイル時間 453 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 45,404 KB
最終ジャッジ日時 2023-09-07 20:08:14
合計ジャッジ時間 6,898 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,924 KB
testcase_01 AC 15 ms
7,960 KB
testcase_02 AC 16 ms
7,928 KB
testcase_03 AC 16 ms
7,808 KB
testcase_04 AC 16 ms
7,816 KB
testcase_05 AC 16 ms
7,792 KB
testcase_06 AC 15 ms
7,868 KB
testcase_07 AC 15 ms
7,992 KB
testcase_08 AC 16 ms
7,876 KB
testcase_09 AC 15 ms
7,976 KB
testcase_10 AC 17 ms
7,924 KB
testcase_11 AC 16 ms
7,892 KB
testcase_12 AC 15 ms
7,884 KB
testcase_13 AC 17 ms
7,864 KB
testcase_14 AC 18 ms
8,336 KB
testcase_15 AC 21 ms
8,248 KB
testcase_16 AC 24 ms
8,560 KB
testcase_17 AC 22 ms
8,224 KB
testcase_18 AC 420 ms
21,448 KB
testcase_19 AC 408 ms
20,692 KB
testcase_20 AC 595 ms
25,632 KB
testcase_21 AC 168 ms
13,916 KB
testcase_22 AC 149 ms
12,956 KB
testcase_23 AC 630 ms
28,260 KB
testcase_24 AC 656 ms
42,244 KB
testcase_25 AC 553 ms
26,024 KB
testcase_26 AC 555 ms
24,620 KB
testcase_27 AC 521 ms
45,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)

N = int(input())
S = input()
G = [ [] for _ in range(len(S))]

for i in range(N-1):
    a,b = sorted(list(map(int, input().split(' '))))
    G[a-1].append(b-1)
    G[b-1].append(a-1)

tc = 0
tw = 0
for s in S:
    if s == 'c':
        tc += 1
    else:
        tw += 1

cc = [0 for _ in range(len(S))]
cw = [0 for _ in range(len(S))]
count = 0

def dfs(current, parent):
    global count
    if S[current] == 'c':
        cc[current] += 1
    else:
        cw[current] += 1

    for adj in G[current]:
        if adj != parent:
            dfs(adj, current)
            cc[current] += cc[adj]
            cw[current] += cw[adj]
            if S[current] == 'w':
                count += cc[adj] * (tw-1-cw[adj])

    if parent != -1 and S[current] == 'w':
        count += (tc-cc[current]) * (cw[current]-1)
        
dfs(0, -1)
print(count)
0