結果

問題 No.1843 Tree ANDistance
ユーザー customaddonecustomaddone
提出日時 2022-03-05 00:47:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 482 ms / 2,000 ms
コード長 2,469 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 87,408 KB
実行使用メモリ 117,212 KB
最終ジャッジ日時 2023-09-26 04:30:51
合計ジャッジ時間 16,511 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 473 ms
117,212 KB
testcase_01 AC 482 ms
117,004 KB
testcase_02 AC 444 ms
117,152 KB
testcase_03 AC 438 ms
116,864 KB
testcase_04 AC 480 ms
116,796 KB
testcase_05 AC 367 ms
113,364 KB
testcase_06 AC 430 ms
116,424 KB
testcase_07 AC 459 ms
112,244 KB
testcase_08 AC 466 ms
115,104 KB
testcase_09 AC 473 ms
114,808 KB
testcase_10 AC 420 ms
114,168 KB
testcase_11 AC 481 ms
116,732 KB
testcase_12 AC 342 ms
112,404 KB
testcase_13 AC 463 ms
113,600 KB
testcase_14 AC 228 ms
80,692 KB
testcase_15 AC 195 ms
80,476 KB
testcase_16 AC 169 ms
80,376 KB
testcase_17 AC 166 ms
80,120 KB
testcase_18 AC 150 ms
79,440 KB
testcase_19 AC 154 ms
80,184 KB
testcase_20 AC 169 ms
80,308 KB
testcase_21 AC 110 ms
76,892 KB
testcase_22 AC 111 ms
77,240 KB
testcase_23 AC 112 ms
77,172 KB
testcase_24 AC 111 ms
76,948 KB
testcase_25 AC 114 ms
77,144 KB
testcase_26 AC 111 ms
76,904 KB
testcase_27 AC 114 ms
76,892 KB
testcase_28 AC 343 ms
108,116 KB
testcase_29 AC 301 ms
99,304 KB
testcase_30 AC 306 ms
100,396 KB
testcase_31 AC 325 ms
111,684 KB
testcase_32 AC 353 ms
108,456 KB
testcase_33 AC 178 ms
88,052 KB
testcase_34 AC 288 ms
102,720 KB
testcase_35 AC 110 ms
77,168 KB
testcase_36 AC 111 ms
77,184 KB
testcase_37 AC 110 ms
77,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
from heapq import heapify, heappop, heappush
import math
from copy import deepcopy
from itertools import combinations, permutations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right

import sys

def input():
    return sys.stdin.readline().rstrip()
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getListGraph():
    return list(map(lambda x:int(x) - 1, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

mod = 10 ** 9 + 7
MOD = 998244353
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')
sys.setrecursionlimit(10000000)
inf = float('inf')
eps = 10 ** (-15)
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]

#############
# Main Code #
#############

"""
UFってこと
"""

# ルートの決定方法はいじれる
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

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

        if x == y:
            return

        # 謎のREが起こったらrootの設定方法を変えよう
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        # if x > y: # よりrootのインデックスが小さい方が親
            # x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parents[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.parents) if x < 0]

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

N = getN()
E = []
for _ in range(N - 1):
    a, b, c = getNM()
    E.append([a - 1, b - 1, c])

ans = 0
for i in range(32):
    U = UnionFind(N)
    for a, b, c in E:
        if c & (1 << i):
            U.union(a, b)
    for j in range(N):
        if j == U.find(j):
            t = U.size(j)
            ans += (1 << i) * (t * (t - 1) // 2)
            ans %= mod

print(ans)
0