結果

問題 No.439 チワワのなる木
ユーザー convexineqconvexineq
提出日時 2021-02-16 07:08:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 576 ms / 5,000 ms
コード長 2,150 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 86,676 KB
実行使用メモリ 132,528 KB
最終ジャッジ日時 2023-10-01 05:09:11
合計ジャッジ時間 9,372 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,144 KB
testcase_01 AC 74 ms
71,064 KB
testcase_02 AC 73 ms
70,916 KB
testcase_03 AC 72 ms
71,048 KB
testcase_04 AC 75 ms
71,184 KB
testcase_05 AC 76 ms
71,300 KB
testcase_06 AC 73 ms
71,284 KB
testcase_07 AC 74 ms
71,068 KB
testcase_08 AC 74 ms
71,256 KB
testcase_09 AC 73 ms
71,016 KB
testcase_10 AC 74 ms
71,124 KB
testcase_11 AC 72 ms
71,216 KB
testcase_12 AC 73 ms
71,040 KB
testcase_13 AC 112 ms
74,936 KB
testcase_14 AC 82 ms
74,892 KB
testcase_15 AC 190 ms
78,476 KB
testcase_16 AC 152 ms
78,788 KB
testcase_17 AC 140 ms
78,948 KB
testcase_18 AC 430 ms
112,060 KB
testcase_19 AC 402 ms
101,404 KB
testcase_20 AC 527 ms
113,980 KB
testcase_21 AC 248 ms
88,596 KB
testcase_22 AC 218 ms
86,796 KB
testcase_23 AC 576 ms
127,372 KB
testcase_24 AC 546 ms
118,512 KB
testcase_25 AC 458 ms
116,216 KB
testcase_26 AC 512 ms
132,528 KB
testcase_27 AC 299 ms
119,440 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] を子方向にマージする際の補正
        return (x[0]+1,x[1],0) if s[parent[v]]=="c" else (x[0],x[1]+1,0)
    

    """
    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),c)
                    acc_r = merge(acc_r,hBU[c])
    
    n = len(g)
    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