結果
| 問題 |
No.439 チワワのなる木
|
| コンテスト | |
| ユーザー |
ckawatak
|
| 提出日時 | 2018-03-29 00:17:41 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 812 ms / 5,000 ms |
| コード長 | 884 bytes |
| コンパイル時間 | 92 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 48,000 KB |
| 最終ジャッジ日時 | 2024-06-25 13:55:09 |
| 合計ジャッジ時間 | 7,124 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
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)
ckawatak