結果

問題 No.439 チワワのなる木
ユーザー nebukuro09
提出日時 2016-10-29 13:30:38
言語 PyPy2
(7.3.15)
結果
TLE  
実行時間 -
コード長 632 bytes
コンパイル時間 1,948 ms
コンパイル使用メモリ 76,416 KB
実行使用メモリ 220,868 KB
最終ジャッジ日時 2024-11-24 17:49:27
合計ジャッジ時間 52,538 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19 RE * 2 TLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

N = input()
S = raw_input()
rinsetsu = [[] for i in xrange(N)]
for _ in xrange(N-1):
    a, b = map(int, raw_input().split())
    rinsetsu[a-1].append(b-1)
    rinsetsu[b-1].append(a-1)

def dfs(char, n, parent):
    return sum(dfs(char, c, n) for c in rinsetsu[n] if c != parent) + (S[n]==char)

ans = 0
for root in xrange(N):
    if S[root] != 'w':
        continue
    c = [dfs('c', child, root) for child in rinsetsu[root]]
    w = [dfs('w', child, root) for child in rinsetsu[root]]
    for i in xrange(len(rinsetsu[root])):
        for j in xrange(i+1, len(rinsetsu[root])):
            ans += c[i]*w[j] + c[j]*w[i]
print ans
0