結果
問題 | No.439 チワワのなる木 |
ユーザー |
![]() |
提出日時 | 2017-12-09 01:05:14 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 694 ms / 5,000 ms |
コード長 | 726 bytes |
コンパイル時間 | 173 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 55,680 KB |
最終ジャッジ日時 | 2024-11-29 20:25:34 |
合計ジャッジ時間 | 6,592 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 28 |
ソースコード
import sys sys.setrecursionlimit(1000000) n = int(input()) s = input() G = [[] for _ in range(n)] for m in range(n-1): x, y = [int(i) for i in input().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))