結果

問題 No.1843 Tree ANDistance
ユーザー asumo0729asumo0729
提出日時 2022-02-18 22:05:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 593 ms / 2,000 ms
コード長 2,477 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 87,312 KB
実行使用メモリ 240,212 KB
最終ジャッジ日時 2023-09-11 19:13:16
合計ジャッジ時間 16,096 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 573 ms
135,668 KB
testcase_01 AC 569 ms
131,968 KB
testcase_02 AC 548 ms
137,532 KB
testcase_03 AC 549 ms
137,656 KB
testcase_04 AC 592 ms
136,904 KB
testcase_05 AC 501 ms
136,348 KB
testcase_06 AC 525 ms
133,956 KB
testcase_07 AC 556 ms
133,332 KB
testcase_08 AC 564 ms
133,280 KB
testcase_09 AC 543 ms
134,820 KB
testcase_10 AC 527 ms
134,516 KB
testcase_11 AC 593 ms
132,880 KB
testcase_12 AC 459 ms
132,216 KB
testcase_13 AC 511 ms
133,900 KB
testcase_14 AC 213 ms
80,856 KB
testcase_15 AC 210 ms
80,588 KB
testcase_16 AC 178 ms
79,716 KB
testcase_17 AC 174 ms
79,692 KB
testcase_18 AC 163 ms
79,260 KB
testcase_19 AC 176 ms
79,772 KB
testcase_20 AC 176 ms
79,424 KB
testcase_21 AC 124 ms
76,436 KB
testcase_22 AC 125 ms
76,492 KB
testcase_23 AC 125 ms
76,496 KB
testcase_24 AC 123 ms
76,468 KB
testcase_25 AC 125 ms
76,496 KB
testcase_26 AC 124 ms
76,440 KB
testcase_27 AC 124 ms
76,312 KB
testcase_28 AC 465 ms
137,388 KB
testcase_29 AC 391 ms
123,252 KB
testcase_30 AC 399 ms
123,308 KB
testcase_31 AC 514 ms
240,212 KB
testcase_32 AC 494 ms
207,612 KB
testcase_33 AC 226 ms
98,200 KB
testcase_34 AC 391 ms
126,464 KB
testcase_35 AC 121 ms
76,640 KB
testcase_36 AC 124 ms
76,472 KB
testcase_37 AC 120 ms
76,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from operator import itemgetter
from collections import defaultdict, deque
import heapq
import bisect
import math
import itertools
import copy

stdin=sys.stdin
sys.setrecursionlimit(10 ** 8)

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
Yp=lambda:print('Yes')
Np=lambda:print('No')
inf = 1 << 60
inf = float('inf')
mod = 10 ** 9 + 7
#mod = 998244353
eps = 1e-9
sortkey1 = itemgetter(0)
sortkey2 = lambda x: (x[0], x[1])

from collections import Counter

class union_find():
    def __init__(self,n):
        self.n=n
        ##親要素のノード番号を格納。par[x]==xのときそのノードは根
        ##親とはその上にノードなし!! 
        self.par=[-1 for i in range(n)]
        self.rank=[0]*(n)

    def find(self,x):
        if self.par[x]<0:
            return x
        else:
            self.par[x]=self.find(self.par[x])
            
            return self.par[x]

    def union(self,x,y):
        x=self.find(x)
        y=self.find(y)

        ##木の高さを比較し、低い方から高い方へ辺をはる
        if x==y:
          return

        if self.par[x]>self.par[y]:
          x,y=y,x
          
        self.par[x]+=self.par[y]
        self.par[y]=x

    ##2つが同じ親かどうか
    def same(self,x,y):
        return self.find(x) == self.find(y)
    
    ##所属のサイズ
    def size(self,x):
      return -self.par[self.find(x)]
      
    ##同じ親のものを表示
    def members(self,x):
      root=self.find(x)
      return [i for i in range(self.n) if self.find(i)==root]
    
    ##親を表示!!
    def roots(self):
      return [i for i, x in enumerate(self.par) if x<0]
    
    def all_group_member(self):
      return {r:self.members(r) for r in self.roots()}

    def group_count(self):
      return len(self.roots())

## uf memo
## 素集合に分けるが、各集合には親というIDが振られているという意識を持つ!!(ABC049_Dより)

###############################################################

N = ip()
adj = []
for _ in range(N - 1):
    a, b, c = lp()
    a -= 1; b -= 1
    adj.append((a, b, c))
    
ans = 0

for j in range(30):
    uf = union_find(N)
    for a, b, c in adj:
        if (c >> j) & 1:
             uf.union(a, b)
    for p in uf.roots():
        s = uf.size(p)
        ans += (2 ** j) * s * (s - 1) // 2
        ans %= mod
print(ans)
0