結果

問題 No.439 チワワのなる木
ユーザー convexineqconvexineq
提出日時 2021-02-16 19:50:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 285 ms / 5,000 ms
コード長 635 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 100,984 KB
最終ジャッジ日時 2023-10-11 10:55:09
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,592 KB
testcase_01 AC 71 ms
71,392 KB
testcase_02 AC 76 ms
71,368 KB
testcase_03 AC 72 ms
71,516 KB
testcase_04 AC 71 ms
71,348 KB
testcase_05 AC 73 ms
71,216 KB
testcase_06 AC 71 ms
71,216 KB
testcase_07 AC 71 ms
71,164 KB
testcase_08 AC 71 ms
71,320 KB
testcase_09 AC 72 ms
71,412 KB
testcase_10 AC 74 ms
71,656 KB
testcase_11 AC 71 ms
71,320 KB
testcase_12 AC 74 ms
71,120 KB
testcase_13 AC 77 ms
71,488 KB
testcase_14 AC 80 ms
71,620 KB
testcase_15 AC 107 ms
77,640 KB
testcase_16 AC 115 ms
78,120 KB
testcase_17 AC 112 ms
78,312 KB
testcase_18 AC 236 ms
91,840 KB
testcase_19 AC 232 ms
91,980 KB
testcase_20 AC 283 ms
97,660 KB
testcase_21 AC 161 ms
83,648 KB
testcase_22 AC 155 ms
82,188 KB
testcase_23 AC 285 ms
98,336 KB
testcase_24 AC 273 ms
98,248 KB
testcase_25 AC 252 ms
99,464 KB
testcase_26 AC 248 ms
100,984 KB
testcase_27 AC 186 ms
98,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
s = input()
g = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

order = [0]
parent = [-1]*n
for v in order:
    for c in g[v]:
        if parent[v] == c: continue
        order.append(c)
        parent[c] = v

ans = 0
C = [0]*n
W = [0]*n
cs = s.count("c")
ws = n-cs
for v in order[::-1]:
    if s[v]=="c":
        C[v] += 1
    else:
        ans += C[v]*(ws-W[v]-1) + W[v]*(cs-C[v])
        W[v] += 1
    p = parent[v]
    if p==-1: break
    if s[p] == "w":
        ans += C[p]*W[v] + W[p]*C[v]
    C[p] += C[v]
    W[p] += W[v]
print(ans)
0