結果

問題 No.439 チワワのなる木
ユーザー convexineqconvexineq
提出日時 2021-02-16 18:51:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 438 ms / 5,000 ms
コード長 2,069 bytes
コンパイル時間 1,986 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 132,772 KB
最終ジャッジ日時 2023-10-11 09:16:48
合計ジャッジ時間 7,470 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,248 KB
testcase_01 AC 66 ms
71,544 KB
testcase_02 AC 65 ms
71,248 KB
testcase_03 AC 65 ms
71,320 KB
testcase_04 AC 65 ms
71,128 KB
testcase_05 AC 69 ms
71,280 KB
testcase_06 AC 71 ms
71,280 KB
testcase_07 AC 70 ms
71,200 KB
testcase_08 AC 67 ms
71,508 KB
testcase_09 AC 70 ms
71,500 KB
testcase_10 AC 69 ms
71,164 KB
testcase_11 AC 65 ms
71,448 KB
testcase_12 AC 66 ms
71,160 KB
testcase_13 AC 73 ms
74,944 KB
testcase_14 AC 75 ms
74,924 KB
testcase_15 AC 127 ms
78,792 KB
testcase_16 AC 137 ms
79,376 KB
testcase_17 AC 129 ms
79,096 KB
testcase_18 AC 329 ms
112,408 KB
testcase_19 AC 333 ms
101,352 KB
testcase_20 AC 394 ms
114,208 KB
testcase_21 AC 203 ms
88,776 KB
testcase_22 AC 191 ms
86,628 KB
testcase_23 AC 438 ms
127,528 KB
testcase_24 AC 395 ms
118,284 KB
testcase_25 AC 390 ms
116,700 KB
testcase_26 AC 422 ms
132,772 KB
testcase_27 AC 275 ms
119,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def rerooting(g,e_M):
    """
    以下を書き換える
    """
    def merge(x,y): #値 x に y をマージ
        return (x[0]+y[0], x[1]+y[1], x[2]+y[2]+x[0]*y[1]+x[1]*y[0])
    def h1(x,v): # x = dp[v][p] を親方向にマージする際の補正
        return (x[0]+1,x[1],0) if s[v]=="c" else (x[0],x[1]+1,0)
    #def h2(x,v): # x = dp[p][v] を子方向にマージする際の補正
    h2 = h1

    """
    dfs_bottom_up: 木DP
    dfs_top_down: rerooting
    """
    def get_order(g):
        n = len(g)
        order = []
        st = [0]
        parent = [-1]*n
        while st:
            v = st.pop()
            order.append(v)
            for c in g[v]:
                if c != parent[v]:
                    st.append(c)
                    parent[c] = v
        return order, parent

    def dfs_bottom_up(BU,hBU,parent,order):
        for i in order[::-1]:
            hBU[i] = h1(BU[i], i)
            if i: BU[parent[i]] = merge(BU[parent[i]], hBU[i])

    def dfs_top_down(hBU,hTD,g,parent,order):
        for v in order:
            acc_l = e_M
            for c in g[v]:
                if c==parent[v]:
                    acc_l = merge(acc_l,hTD[v])
                else:
                    hTD[c] = acc_l
                    acc_l = merge(acc_l,hBU[c])
            acc_r = e_M
            for c in g[v][::-1]:
                if c==parent[v]:
                    acc_r = merge(hTD[v],acc_r)
                else:
                    hTD[c] = h2(merge(hTD[c],acc_r),v)
                    acc_r = merge(acc_r,hBU[c])
    
    order, parent = get_order(g)
    BU = [e_M]*n
    hBU = [e_M]*n
    hTD = [e_M]*n
    dfs_bottom_up(BU,hBU,parent,order)
    dfs_top_down(hBU,hTD,g,parent,order)
    
    for i in range(n):
        BU[i] = merge(BU[i],hTD[i])
    return BU

n = int(input())
s = input()
g = [[] for _ in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

BU = rerooting(g,(0,0,0))
ans = 0
for i in range(n):
    if s[i] == "w":
        ans += BU[i][2]
print(ans)
0