結果

問題 No.1843 Tree ANDistance
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-02-19 16:33:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 614 ms / 2,000 ms
コード長 1,051 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 86,936 KB
実行使用メモリ 119,380 KB
最終ジャッジ日時 2023-09-11 20:43:18
合計ジャッジ時間 17,666 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 614 ms
119,040 KB
testcase_01 AC 608 ms
119,380 KB
testcase_02 AC 567 ms
108,392 KB
testcase_03 AC 569 ms
118,556 KB
testcase_04 AC 608 ms
118,956 KB
testcase_05 AC 501 ms
114,764 KB
testcase_06 AC 554 ms
118,376 KB
testcase_07 AC 579 ms
109,980 KB
testcase_08 AC 591 ms
117,476 KB
testcase_09 AC 537 ms
116,124 KB
testcase_10 AC 531 ms
115,928 KB
testcase_11 AC 600 ms
119,140 KB
testcase_12 AC 465 ms
115,148 KB
testcase_13 AC 518 ms
109,020 KB
testcase_14 AC 291 ms
85,360 KB
testcase_15 AC 289 ms
85,044 KB
testcase_16 AC 261 ms
84,288 KB
testcase_17 AC 260 ms
83,928 KB
testcase_18 AC 236 ms
83,404 KB
testcase_19 AC 254 ms
84,392 KB
testcase_20 AC 257 ms
84,200 KB
testcase_21 AC 194 ms
79,552 KB
testcase_22 AC 187 ms
79,600 KB
testcase_23 AC 187 ms
79,552 KB
testcase_24 AC 189 ms
79,536 KB
testcase_25 AC 188 ms
79,340 KB
testcase_26 AC 187 ms
79,452 KB
testcase_27 AC 187 ms
79,368 KB
testcase_28 AC 441 ms
111,236 KB
testcase_29 AC 399 ms
101,632 KB
testcase_30 AC 415 ms
103,120 KB
testcase_31 AC 444 ms
113,192 KB
testcase_32 AC 458 ms
111,376 KB
testcase_33 AC 273 ms
93,080 KB
testcase_34 AC 406 ms
106,196 KB
testcase_35 AC 191 ms
79,672 KB
testcase_36 AC 193 ms
79,640 KB
testcase_37 AC 198 ms
79,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from ctypes import Union


class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]


n = int(input())
ans = 0
mod = 10**9+7

edges = [list(map(int,input().split())) for i in range(n-1)]
for i in range(30):
    uf = Unionfind(n)
    for a,b,c in edges:
        if c >> i & 1:
            uf.union(a-1,b-1)

    for ind in range(n):
        if uf.find(ind) != ind:
            continue
        s = uf.size(ind)
        ans += (s*(s-1)//2)<<i
        ans %= mod
print(ans)
0