結果

問題 No.1769 Don't Stop the Game
ユーザー first_vilfirst_vil
提出日時 2021-11-03 14:57:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 909 ms / 3,000 ms
コード長 1,671 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 86,904 KB
実行使用メモリ 163,420 KB
最終ジャッジ日時 2023-09-12 05:01:36
合計ジャッジ時間 18,936 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,908 KB
testcase_01 AC 94 ms
71,840 KB
testcase_02 AC 97 ms
71,632 KB
testcase_03 AC 118 ms
77,628 KB
testcase_04 AC 104 ms
72,500 KB
testcase_05 AC 119 ms
77,744 KB
testcase_06 AC 129 ms
78,356 KB
testcase_07 AC 129 ms
77,940 KB
testcase_08 AC 671 ms
113,444 KB
testcase_09 AC 504 ms
104,504 KB
testcase_10 AC 702 ms
127,000 KB
testcase_11 AC 520 ms
103,508 KB
testcase_12 AC 750 ms
120,324 KB
testcase_13 AC 768 ms
121,172 KB
testcase_14 AC 767 ms
120,980 KB
testcase_15 AC 756 ms
120,868 KB
testcase_16 AC 798 ms
121,896 KB
testcase_17 AC 792 ms
122,780 KB
testcase_18 AC 874 ms
145,704 KB
testcase_19 AC 897 ms
156,988 KB
testcase_20 AC 902 ms
157,620 KB
testcase_21 AC 909 ms
157,300 KB
testcase_22 AC 900 ms
157,480 KB
testcase_23 AC 748 ms
121,192 KB
testcase_24 AC 768 ms
121,564 KB
testcase_25 AC 329 ms
131,040 KB
testcase_26 AC 473 ms
162,532 KB
testcase_27 AC 335 ms
120,732 KB
testcase_28 AC 488 ms
163,420 KB
testcase_29 AC 521 ms
160,856 KB
testcase_30 AC 523 ms
157,776 KB
権限があれば一括ダウンロードができます

ソースコード

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 = [0]*n,[0]*n
st = deque([0])
now = 0
while st:
    cur = st.pop()
    if siz[cur]:
        for to,weight in g[cur]:
            siz[cur] += siz[to]
    else:
        st.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
                st.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,cnt,tmp_y,tmp_cnt = [0]*n,[0]*n,[-1]*n,[-1]*n
ans = n*(n-1)

p20 = 1<<20
mask = p20-1
st.append(0)
while st:
    tmp = st.pop()
    cur,step = tmp&mask,tmp//p20
    
    if step == 0:
        ans -= y[x[cur]]
        ans -= siz[cur] * cnt[x[cur]]
        tmp_cnt[cur] = cnt[x[cur]]
        tmp_y[cur] = y[x[cur]]
    
    if step < len(g[cur]):
        cnt[x[cur]] = 1
        y[x[cur]] = n - siz[g[cur][step][0]]
        st.append(cur + p20*(step+1))
        st.append(g[cur][step][0])
    else:
        cnt[x[cur]] = tmp_cnt[cur] + 1
        y[x[cur]] = tmp_y[cur] + siz[cur]

print(ans)
0