結果
問題 |
No.439 チワワのなる木
|
ユーザー |
![]() |
提出日時 | 2016-04-25 20:44:39 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 778 bytes |
コンパイル時間 | 230 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 31,812 KB |
最終ジャッジ日時 | 2024-10-04 15:45:59 |
合計ジャッジ時間 | 10,981 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 6 WA * 12 TLE * 1 -- * 9 |
ソースコード
from collections import defaultdict as ddict import sys sys.setrecursionlimit(100000) mod = 1000000007 def dfs(now, pre): ret = [0, 0, 0] for nxt in G[now]: if nxt == pre: continue child = dfs(nxt, now) for i in range(3): ret[i] += child[i] if s[now] == 'c': ret[2] += ret[1] else: ret[1] += ret[0] ret[0] += 1 for i in range(3): ret[i] %= mod return ret ################################# N = int(input()) s = input() G = ddict(list) for i in range(N - 1): a, b = list(map(int, input().split())) a -= 1; b -= 1 G[a].append(b) G[b].append(a) ans = 0 for i in range(N): if s[i] == 'c': ans += dfs(i, -1)[2] ans %= mod print(ans)