結果
問題 | No.1769 Don't Stop the Game |
ユーザー | first_vil |
提出日時 | 2021-10-27 13:36:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,439 ms / 3,000 ms |
コード長 | 2,852 bytes |
コンパイル時間 | 480 ms |
コンパイル使用メモリ | 87,136 KB |
実行使用メモリ | 176,564 KB |
最終ジャッジ日時 | 2023-09-12 04:48:41 |
合計ジャッジ時間 | 28,104 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 97 ms
71,512 KB |
testcase_01 | AC | 95 ms
71,552 KB |
testcase_02 | AC | 95 ms
71,684 KB |
testcase_03 | AC | 137 ms
77,872 KB |
testcase_04 | AC | 104 ms
72,316 KB |
testcase_05 | AC | 131 ms
77,532 KB |
testcase_06 | AC | 148 ms
78,176 KB |
testcase_07 | AC | 162 ms
78,384 KB |
testcase_08 | AC | 1,041 ms
112,572 KB |
testcase_09 | AC | 791 ms
105,176 KB |
testcase_10 | AC | 1,089 ms
125,736 KB |
testcase_11 | AC | 764 ms
102,856 KB |
testcase_12 | AC | 1,226 ms
119,344 KB |
testcase_13 | AC | 1,223 ms
120,244 KB |
testcase_14 | AC | 1,279 ms
120,052 KB |
testcase_15 | AC | 1,235 ms
119,944 KB |
testcase_16 | AC | 1,274 ms
119,612 KB |
testcase_17 | AC | 1,331 ms
124,072 KB |
testcase_18 | AC | 1,396 ms
145,392 KB |
testcase_19 | AC | 1,408 ms
157,660 KB |
testcase_20 | AC | 1,437 ms
158,300 KB |
testcase_21 | AC | 1,423 ms
158,288 KB |
testcase_22 | AC | 1,439 ms
157,820 KB |
testcase_23 | AC | 1,188 ms
120,648 KB |
testcase_24 | AC | 1,194 ms
120,556 KB |
testcase_25 | AC | 402 ms
130,896 KB |
testcase_26 | AC | 560 ms
176,564 KB |
testcase_27 | AC | 380 ms
124,232 KB |
testcase_28 | AC | 507 ms
154,928 KB |
testcase_29 | AC | 593 ms
162,432 KB |
testcase_30 | AC | 603 ms
159,808 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 = [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) # st.pop()[1] の0bit目は 0,1 = false/true # 1bit目は 0,1 = 最初,f(true)の直後 # を表す st1.append((0,0)) st2 = deque([]) while st1: cur1,tmp = st1.pop() flag,step = tmp&1,tmp&2 if step == 0: st1.append((cur1, flag | 2)) for i in range(len(g[cur1])): st1.append((g[cur1][i][0], int(i != 0))) else: for i in range(1,len(g[cur1])): st2.append(g[cur1][i][0]) #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)