結果
| 問題 | 
                            No.439 チワワのなる木
                             | 
                    
| コンテスト | |
| ユーザー | 
                             okaduki
                         | 
                    
| 提出日時 | 2017-12-09 00:59:24 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                RE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 683 bytes | 
| コンパイル時間 | 139 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 36,736 KB | 
| 最終ジャッジ日時 | 2024-11-29 19:59:44 | 
| 合計ジャッジ時間 | 6,936 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 25 RE * 3 | 
ソースコード
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))
            
            
            
        
            
okaduki