結果

問題 No.1843 Tree ANDistance
ユーザー hir355hir355
提出日時 2022-02-18 22:23:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 895 ms / 2,000 ms
コード長 1,235 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 87,228 KB
実行使用メモリ 179,668 KB
最終ジャッジ日時 2023-09-11 19:30:13
合計ジャッジ時間 17,769 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 837 ms
179,668 KB
testcase_01 AC 882 ms
179,476 KB
testcase_02 AC 810 ms
179,236 KB
testcase_03 AC 817 ms
178,688 KB
testcase_04 AC 895 ms
179,628 KB
testcase_05 AC 572 ms
178,244 KB
testcase_06 AC 621 ms
178,508 KB
testcase_07 AC 847 ms
175,344 KB
testcase_08 AC 808 ms
177,116 KB
testcase_09 AC 762 ms
174,020 KB
testcase_10 AC 764 ms
172,504 KB
testcase_11 AC 888 ms
176,068 KB
testcase_12 AC 497 ms
168,956 KB
testcase_13 AC 545 ms
170,780 KB
testcase_14 AC 187 ms
81,068 KB
testcase_15 AC 186 ms
81,208 KB
testcase_16 AC 147 ms
79,784 KB
testcase_17 AC 143 ms
79,312 KB
testcase_18 AC 138 ms
78,528 KB
testcase_19 AC 132 ms
81,108 KB
testcase_20 AC 143 ms
79,364 KB
testcase_21 AC 78 ms
71,488 KB
testcase_22 AC 77 ms
71,420 KB
testcase_23 AC 78 ms
71,092 KB
testcase_24 AC 78 ms
71,524 KB
testcase_25 AC 77 ms
71,340 KB
testcase_26 AC 77 ms
71,316 KB
testcase_27 AC 77 ms
71,512 KB
testcase_28 AC 380 ms
156,344 KB
testcase_29 AC 295 ms
131,312 KB
testcase_30 AC 291 ms
132,488 KB
testcase_31 AC 359 ms
163,520 KB
testcase_32 AC 371 ms
157,772 KB
testcase_33 AC 150 ms
104,412 KB
testcase_34 AC 260 ms
141,148 KB
testcase_35 AC 79 ms
71,384 KB
testcase_36 AC 80 ms
71,324 KB
testcase_37 AC 78 ms
71,452 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] * n
    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