結果

問題 No.439 チワワのなる木
ユーザー ckawatakckawatak
提出日時 2018-03-29 00:00:52
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 841 bytes
コンパイル時間 981 ms
コンパイル使用メモリ 86,652 KB
実行使用メモリ 98,404 KB
最終ジャッジ日時 2023-09-07 20:07:44
合計ジャッジ時間 7,417 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,264 KB
testcase_01 AC 70 ms
70,936 KB
testcase_02 AC 70 ms
71,144 KB
testcase_03 AC 70 ms
71,248 KB
testcase_04 AC 69 ms
71,108 KB
testcase_05 AC 72 ms
71,060 KB
testcase_06 AC 69 ms
71,116 KB
testcase_07 AC 72 ms
71,164 KB
testcase_08 AC 73 ms
71,164 KB
testcase_09 AC 70 ms
71,108 KB
testcase_10 AC 75 ms
71,172 KB
testcase_11 AC 69 ms
71,060 KB
testcase_12 AC 69 ms
71,288 KB
testcase_13 AC 72 ms
71,144 KB
testcase_14 AC 75 ms
71,020 KB
testcase_15 AC 109 ms
77,936 KB
testcase_16 AC 116 ms
78,040 KB
testcase_17 AC 116 ms
77,968 KB
testcase_18 AC 396 ms
92,008 KB
testcase_19 AC 316 ms
89,208 KB
testcase_20 AC 455 ms
96,220 KB
testcase_21 AC 206 ms
82,668 KB
testcase_22 AC 248 ms
84,664 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 AC 290 ms
94,960 KB
testcase_26 AC 296 ms
94,892 KB
testcase_27 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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