結果

問題 No.1843 Tree ANDistance
ユーザー roarisroaris
提出日時 2022-02-28 01:43:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,827 ms / 2,000 ms
コード長 1,410 bytes
コンパイル時間 2,040 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 309,328 KB
最終ジャッジ日時 2023-09-20 09:03:42
合計ジャッジ時間 37,398 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,766 ms
299,284 KB
testcase_01 AC 1,776 ms
298,800 KB
testcase_02 AC 1,813 ms
308,664 KB
testcase_03 AC 1,827 ms
309,328 KB
testcase_04 AC 1,731 ms
307,048 KB
testcase_05 AC 1,167 ms
266,324 KB
testcase_06 AC 1,257 ms
279,324 KB
testcase_07 AC 1,675 ms
290,856 KB
testcase_08 AC 1,645 ms
294,504 KB
testcase_09 AC 1,677 ms
288,020 KB
testcase_10 AC 1,620 ms
280,172 KB
testcase_11 AC 1,704 ms
295,624 KB
testcase_12 AC 987 ms
263,384 KB
testcase_13 AC 1,192 ms
262,744 KB
testcase_14 AC 229 ms
80,228 KB
testcase_15 AC 236 ms
80,208 KB
testcase_16 AC 198 ms
79,644 KB
testcase_17 AC 188 ms
79,536 KB
testcase_18 AC 161 ms
78,496 KB
testcase_19 AC 181 ms
79,436 KB
testcase_20 AC 183 ms
79,504 KB
testcase_21 AC 99 ms
71,588 KB
testcase_22 AC 102 ms
71,408 KB
testcase_23 AC 98 ms
71,668 KB
testcase_24 AC 98 ms
71,864 KB
testcase_25 AC 95 ms
71,804 KB
testcase_26 AC 97 ms
71,864 KB
testcase_27 AC 100 ms
71,544 KB
testcase_28 AC 862 ms
234,332 KB
testcase_29 AC 645 ms
201,888 KB
testcase_30 AC 531 ms
194,872 KB
testcase_31 AC 923 ms
242,808 KB
testcase_32 AC 865 ms
231,760 KB
testcase_33 AC 298 ms
128,708 KB
testcase_34 AC 623 ms
212,240 KB
testcase_35 AC 101 ms
71,388 KB
testcase_36 AC 100 ms
71,912 KB
testcase_37 AC 102 ms
71,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N = int(input())
ABC = [tuple(map(int, input().split())) for _ in range(N-1)]
ans = 0
MOD = 10**9+7

for i in range(35):
    uf = Unionfind(N)
    
    for A, B, C in ABC:
        if (C>>i)&1:
            uf.unite(A-1, B-1)
        
    d = defaultdict(int)
    
    for v in range(N):
        d[uf.root(v)] += 1
    
    for v in d.values():
        ans += (1<<i)*v*(v-1)//2
        ans %= MOD

print(ans)
0