結果

問題 No.1843 Tree ANDistance
ユーザー hir355hir355
提出日時 2022-02-18 22:21:44
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,236 bytes
コンパイル時間 336 ms
コンパイル使用メモリ 86,720 KB
実行使用メモリ 158,304 KB
最終ジャッジ日時 2023-09-11 19:27:53
合計ジャッジ時間 17,045 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 71 ms
71,140 KB
testcase_22 AC 71 ms
71,100 KB
testcase_23 AC 70 ms
70,808 KB
testcase_24 AC 71 ms
70,808 KB
testcase_25 AC 69 ms
71,160 KB
testcase_26 AC 69 ms
71,300 KB
testcase_27 AC 69 ms
71,132 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 AC 69 ms
71,196 KB
testcase_36 AC 70 ms
71,236 KB
testcase_37 AC 70 ms
71,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)
 
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
 
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n = int(input())
uf = [UnionFind(n) for _ in range(32)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    for j in range(32):
        if (c >> j) & 1:
            uf[j].unite(a - 1, b - 1)
ans = 0
for i, u in enumerate(uf):
    d = [0] * 32
    for j in range(n):
        j = u.find(j)
        if d[j]:
            continue
        d[j] = 1
        s = u.size[j]
        ans += (s * (s - 1) // 2) << i
print(ans % (10 ** 9 + 7))
0