結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-10-27 22:56:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,751 bytes
コンパイル時間 407 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 213,804 KB
最終ジャッジ日時 2023-09-12 04:51:08
合計ジャッジ時間 23,840 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
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 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
mi = lambda: map(int,input().split())
li = lambda: list(mi())
from collections import deque

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
    a,b,c = mi()
    a,b = a-1,b-1
    g[a].append((b,c))
    g[b].append((a,c))
    
siz,x,par = [0]*n,[0]*n,[-1]*n
st1 = deque([0])
while st1:
    cur = st1.pop()
    if siz[cur]:
        g[cur].sort(key = lambda val: -siz[val[0]])
        for to,weight in g[cur]:
            siz[cur] += siz[to]
    else:
        st1.append(cur)
        par_idx = -1
        for i in range(len(g[cur])):
            to,weight = g[cur][i]
            if siz[to]:
                par_idx = i
            else:
                x[to] = x[cur] ^ weight
                par[to] = cur
                st1.append(to)
        if par_idx != -1:
            g[cur][-1],g[cur][par_idx] = g[cur][par_idx],g[cur][-1]
            g[cur].pop()
        siz[cur] = 1
    
sorted_x = sorted(set(x))
for i in range(n):
    ok,ng = 0,len(sorted_x)
    while ng-ok > 1:
        m = (ok+ng) >> 1
        if sorted_x[m] <= x[i]:
            ok = m
        else:
            ng = m
    x[i] = ok
    
y_sum,cnt_sum = [0]*len(sorted_x),[0]*len(sorted_x)
y,cnt = [-1]*n, [-1]*n
ans = n*(n-1)

st1.append(0)
st2 = deque([])
p30,p31 = 1<<30,1<<31
mask = p30-1
while st1:
    tmp = st1.pop()
    cur1,flag,step = tmp&mask,tmp&p30,tmp&p31

    if step == 0:
        st1.append(cur1 | flag | p31)
        for i in range(len(g[cur1])):
            st1.append(g[cur1][i][0] | (int(i != 0) * p30))
    else:
        st2.append(cur1)
        #dfs1-begin
        while st2:
            cur2 = st2.pop()
            y_sum[x[cur2]] += y[cur2];
            cnt_sum[x[cur2]] += cnt[cur2];
            for to,weight in g[cur2]:
                st2.append(to)
        #dfs1-end
        
        if y[cur1] == -1:
            y[cur1] = siz[cur1] - y_sum[x[cur1]]
            cnt[cur1] = 1 - cnt_sum[x[cur1]]
        ans -= y_sum[x[cur1]]#case-1
        y_sum[x[cur1]] += y[cur1]
        cnt_sum[x[cur1]] += cnt[cur1]

        if cur1 == 0:
            for j in range(len(sorted_x)):
                if j != x[cur1]:
                    ans -= y_sum[j] * cnt_sum[j]#case-3
        else:
            ans -= cnt_sum[x[par[cur1]]] * (n - siz[cur1])#case-2
            ans -= y_sum[x[par[cur1]]] * cnt_sum[x[par[cur1]]]#case-3
            ans += siz[cur1]#case-3
        
        if flag:
            st2.append(cur1)
            #dfs1-begin
            while st2:
                cur2 = st2.pop()
                y_sum[x[cur2]] -= y[cur2];
                cnt_sum[x[cur2]] -= cnt[cur2];
                for to,weight in g[cur2]:
                    st2.append(to)
            #dfs1-end

print(ans)
0