結果
問題 | No.1769 Don't Stop the Game |
ユーザー | first_vil |
提出日時 | 2021-10-27 23:34:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,076 ms / 3,000 ms |
コード長 | 2,674 bytes |
コンパイル時間 | 170 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 172,356 KB |
最終ジャッジ日時 | 2024-06-29 17:50:23 |
合計ジャッジ時間 | 20,056 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,272 KB |
testcase_01 | AC | 42 ms
54,656 KB |
testcase_02 | AC | 43 ms
54,144 KB |
testcase_03 | AC | 77 ms
72,820 KB |
testcase_04 | AC | 54 ms
56,320 KB |
testcase_05 | AC | 80 ms
70,912 KB |
testcase_06 | AC | 98 ms
76,800 KB |
testcase_07 | AC | 104 ms
76,928 KB |
testcase_08 | AC | 757 ms
114,944 KB |
testcase_09 | AC | 593 ms
105,808 KB |
testcase_10 | AC | 799 ms
129,180 KB |
testcase_11 | AC | 588 ms
105,268 KB |
testcase_12 | AC | 898 ms
123,048 KB |
testcase_13 | AC | 895 ms
124,168 KB |
testcase_14 | AC | 878 ms
124,200 KB |
testcase_15 | AC | 860 ms
123,616 KB |
testcase_16 | AC | 878 ms
123,312 KB |
testcase_17 | AC | 923 ms
127,952 KB |
testcase_18 | AC | 1,044 ms
149,288 KB |
testcase_19 | AC | 1,069 ms
162,068 KB |
testcase_20 | AC | 1,076 ms
162,572 KB |
testcase_21 | AC | 1,035 ms
162,488 KB |
testcase_22 | AC | 983 ms
162,052 KB |
testcase_23 | AC | 820 ms
124,012 KB |
testcase_24 | AC | 816 ms
124,320 KB |
testcase_25 | AC | 329 ms
135,352 KB |
testcase_26 | AC | 454 ms
172,356 KB |
testcase_27 | AC | 325 ms
130,248 KB |
testcase_28 | AC | 450 ms
165,932 KB |
testcase_29 | AC | 513 ms
166,060 KB |
testcase_30 | AC | 537 ms
154,760 KB |
ソースコード
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,In,Out,when = [0]*n,[0]*n,[-1]*n,[0]*n,[0]*n,[0]*n st = deque([0]) now = 0 while st: cur = st.pop() if siz[cur]: g[cur].sort(key = lambda val: -siz[val[0]]) 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 par[to] = cur 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_sum,cnt_sum = [0]*len(sorted_x),[0]*len(sorted_x) y,cnt = [-1]*n, [-1]*n ans = n*(n-1) st.append(0) p30,p31 = 1<<30,1<<31 mask = p30-1 while st: tmp = st.pop() cur1,flag,step = tmp&mask,tmp&p30,tmp&p31 if step == 0: In[cur1] = now when[now] = cur1 now += 1 st.append(cur1 | flag | p31) for i in range(len(g[cur1])): st.append(g[cur1][i][0] | (int(i != 0) * p30)) else: Out[cur1] = now if len(g[cur1]) > 1: for t in range(In[g[cur1][-1][0]],Out[g[cur1][1][0]]): cur2 = when[t] y_sum[x[cur2]] += y[cur2] cnt_sum[x[cur2]] += cnt[cur2] 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: for t in range(In[cur1],Out[cur1]): cur2 = when[t] y_sum[x[cur2]] -= y[cur2] cnt_sum[x[cur2]] -= cnt[cur2] print(ans)