結果

問題 No.439 チワワのなる木
ユーザー ckawatakckawatak
提出日時 2018-03-24 22:46:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,153 bytes
コンパイル時間 368 ms
コンパイル使用メモリ 86,808 KB
実行使用メモリ 126,748 KB
最終ジャッジ日時 2023-09-07 08:59:25
合計ジャッジ時間 7,810 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
72,352 KB
testcase_01 AC 88 ms
71,920 KB
testcase_02 AC 85 ms
72,268 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 388 ms
125,220 KB
testcase_25 AC 238 ms
102,892 KB
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import reduce
from math import factorial

N = int(input())
S = input()
G = {}
H = {}
for i in range(N-1):
    a,b = sorted(list(map(int, input().split(' '))))
    if a-1 in G:
        G[a-1].append(b-1)
    elif a-1 not in G:
        G[a-1] = [b-1]
    if b-1 in H:
        H[b-1].append(a-1)
    else:
        H[b-1] = [a-1]
        
for i in G:
    G[i] = sorted(G[i])
for i in H:
    H[i] = sorted(H[i])

def dfs(graph, node, started, ws, wss):
    if node not in graph:
        if S[node] == 'w':
            ws += 1
        wss.append(ws)
        return
    if S[node] == 'w':
        ws += 1
    else:
        if started:
            started = False
        else:
            wss.append(ws)
            return
    for n in graph[node]:
        dfs(graph, n, started, ws, wss)

def nc2(n):
    if n < 2:
        return 0
    f = factorial
    return f(n) // f(2) // f(n-2)

forward = []
for i in range(len(S)):
    if S[i] == 'c':
        dfs(G, i, True, 0, forward)

backward = []
for i in reversed(range(len(S))):
    if S[i] == 'c':
        dfs(H, i, True, 0, backward)

print(reduce(lambda x,y: x+nc2(y), forward + backward, 0))
0