結果

問題 No.1843 Tree ANDistance
ユーザー shinichishinichi
提出日時 2022-02-19 01:38:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 506 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 115,672 KB
最終ジャッジ日時 2023-09-11 20:27:28
合計ジャッジ時間 13,013 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 484 ms
109,436 KB
testcase_01 AC 481 ms
110,000 KB
testcase_02 AC 458 ms
115,640 KB
testcase_03 AC 462 ms
115,672 KB
testcase_04 AC 506 ms
109,328 KB
testcase_05 AC 410 ms
115,632 KB
testcase_06 AC 448 ms
115,640 KB
testcase_07 AC 470 ms
114,228 KB
testcase_08 AC 478 ms
112,572 KB
testcase_09 AC 426 ms
113,188 KB
testcase_10 AC 439 ms
111,532 KB
testcase_11 AC 503 ms
107,728 KB
testcase_12 AC 357 ms
111,792 KB
testcase_13 AC 406 ms
112,912 KB
testcase_14 AC 177 ms
78,960 KB
testcase_15 AC 175 ms
79,096 KB
testcase_16 AC 145 ms
78,388 KB
testcase_17 AC 142 ms
78,088 KB
testcase_18 AC 118 ms
77,732 KB
testcase_19 AC 138 ms
78,824 KB
testcase_20 AC 142 ms
78,612 KB
testcase_21 AC 75 ms
71,372 KB
testcase_22 AC 75 ms
71,344 KB
testcase_23 AC 77 ms
71,272 KB
testcase_24 AC 76 ms
71,460 KB
testcase_25 AC 76 ms
71,316 KB
testcase_26 AC 76 ms
71,284 KB
testcase_27 AC 76 ms
71,420 KB
testcase_28 AC 370 ms
106,744 KB
testcase_29 AC 292 ms
97,428 KB
testcase_30 AC 300 ms
98,576 KB
testcase_31 AC 343 ms
110,648 KB
testcase_32 AC 344 ms
107,528 KB
testcase_33 AC 158 ms
86,932 KB
testcase_34 AC 276 ms
101,092 KB
testcase_35 AC 73 ms
71,248 KB
testcase_36 AC 75 ms
71,116 KB
testcase_37 AC 75 ms
71,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind(object):
    def __init__(self, size):
        self.parent = [-1] * size

    def root(self, x):
        if self.parent[x] < 0:
            return x
        self.parent[x] = self.root(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def size(self, x):
        return -self.parent[self.root(x)]

N = int(input())
data = []
for _ in range(N-1):
    data.append(tuple(map(int, input().split())))

ans = 0
mod = 10**9+7
for k in range(30):
    uf = UnionFind(N)
    for a, b, c in data:
        if c >> k & 1:
            uf.union(a-1, b-1)
    for r in range(N):
        if uf.parent[r] < 0:
            ans += (1 << k) * uf.size(r) * (uf.size(r) - 1) // 2
            ans %= mod

print(ans)




0