結果
問題 | No.1843 Tree ANDistance |
ユーザー | Shirotsume |
提出日時 | 2022-02-18 21:50:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 806 ms / 2,000 ms |
コード長 | 2,238 bytes |
コンパイル時間 | 165 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 259,612 KB |
最終ジャッジ日時 | 2024-06-29 08:43:58 |
合計ジャッジ時間 | 17,871 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 806 ms
210,704 KB |
testcase_01 | AC | 767 ms
213,212 KB |
testcase_02 | AC | 749 ms
253,628 KB |
testcase_03 | AC | 757 ms
239,848 KB |
testcase_04 | AC | 799 ms
233,764 KB |
testcase_05 | AC | 586 ms
226,704 KB |
testcase_06 | AC | 668 ms
244,028 KB |
testcase_07 | AC | 773 ms
203,844 KB |
testcase_08 | AC | 784 ms
214,136 KB |
testcase_09 | AC | 708 ms
218,624 KB |
testcase_10 | AC | 704 ms
219,368 KB |
testcase_11 | AC | 801 ms
226,396 KB |
testcase_12 | AC | 568 ms
219,096 KB |
testcase_13 | AC | 622 ms
230,488 KB |
testcase_14 | AC | 181 ms
78,244 KB |
testcase_15 | AC | 176 ms
78,468 KB |
testcase_16 | AC | 134 ms
77,440 KB |
testcase_17 | AC | 142 ms
77,688 KB |
testcase_18 | AC | 116 ms
76,460 KB |
testcase_19 | AC | 136 ms
78,208 KB |
testcase_20 | AC | 141 ms
77,184 KB |
testcase_21 | AC | 47 ms
52,352 KB |
testcase_22 | AC | 45 ms
52,608 KB |
testcase_23 | AC | 51 ms
52,736 KB |
testcase_24 | AC | 42 ms
52,864 KB |
testcase_25 | AC | 40 ms
52,608 KB |
testcase_26 | AC | 39 ms
52,864 KB |
testcase_27 | AC | 40 ms
52,992 KB |
testcase_28 | AC | 600 ms
258,484 KB |
testcase_29 | AC | 503 ms
234,768 KB |
testcase_30 | AC | 477 ms
217,000 KB |
testcase_31 | AC | 615 ms
259,612 KB |
testcase_32 | AC | 662 ms
258,876 KB |
testcase_33 | AC | 209 ms
138,500 KB |
testcase_34 | AC | 482 ms
249,956 KB |
testcase_35 | AC | 39 ms
52,736 KB |
testcase_36 | AC | 39 ms
52,480 KB |
testcase_37 | AC | 41 ms
52,736 KB |
ソースコード
class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) x=self.leader(a) y=self.leader(b) if x==y: return x if (-self.parent_or_size[x]<-self.parent_or_size[y]): x,y=y,x self.parent_or_size[x]+=self.parent_or_size[y] self.parent_or_size[y]=x return x def same(self,a,b): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n) return self.leader(a)==self.leader(b) def leader(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) if (self.parent_or_size[a]<0): return a self.parent_or_size[a]=self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self,a): assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n) return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf=[0 for i in range(self.n)] group_size=[0 for i in range(self.n)] for i in range(self.n): leader_buf[i]=self.leader(i) group_size[leader_buf[i]]+=1 result=[[] for i in range(self.n)] for i in range(self.n): result[leader_buf[i]].append(i) result2=[] for i in range(self.n): if len(result[i])>0: result2.append(result[i]) return result2 n = int(input()) mod = 10 ** 9 + 7 abc = [list(map(int,input().split())) for _ in range(n - 1)] ans = 0 bi2 = 1 for bit in range(32): U = dsu(n + 1) for i in range(n - 1): a, b, c = abc[i] a -= 1; b -= 1 if 1 & (c >> bit): U.merge(a, b) s = set() for i in range(n): x = U.leader(i) if x in s: continue else: s.add(x) c = U.size(i) ans += bi2 * c * (c - 1) // 2 ans %= mod bi2 *= 2 bi2 %= mod print(ans)