結果

問題 No.1843 Tree ANDistance
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-02-19 16:34:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 626 ms / 2,000 ms
コード長 1,088 bytes
コンパイル時間 408 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 119,760 KB
最終ジャッジ日時 2023-09-11 20:43:37
合計ジャッジ時間 18,222 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 616 ms
119,628 KB
testcase_01 AC 626 ms
119,760 KB
testcase_02 AC 581 ms
108,528 KB
testcase_03 AC 573 ms
118,696 KB
testcase_04 AC 613 ms
118,904 KB
testcase_05 AC 510 ms
115,460 KB
testcase_06 AC 560 ms
118,252 KB
testcase_07 AC 593 ms
109,224 KB
testcase_08 AC 608 ms
117,268 KB
testcase_09 AC 552 ms
116,524 KB
testcase_10 AC 547 ms
115,884 KB
testcase_11 AC 626 ms
119,208 KB
testcase_12 AC 484 ms
115,080 KB
testcase_13 AC 539 ms
107,456 KB
testcase_14 AC 309 ms
85,232 KB
testcase_15 AC 307 ms
85,236 KB
testcase_16 AC 277 ms
84,260 KB
testcase_17 AC 274 ms
84,196 KB
testcase_18 AC 254 ms
83,580 KB
testcase_19 AC 268 ms
84,732 KB
testcase_20 AC 277 ms
84,476 KB
testcase_21 AC 200 ms
79,692 KB
testcase_22 AC 201 ms
79,704 KB
testcase_23 AC 205 ms
79,668 KB
testcase_24 AC 205 ms
79,744 KB
testcase_25 AC 209 ms
79,784 KB
testcase_26 AC 204 ms
79,652 KB
testcase_27 AC 202 ms
79,464 KB
testcase_28 AC 467 ms
111,512 KB
testcase_29 AC 413 ms
102,420 KB
testcase_30 AC 427 ms
103,204 KB
testcase_31 AC 456 ms
113,672 KB
testcase_32 AC 463 ms
111,940 KB
testcase_33 AC 288 ms
93,132 KB
testcase_34 AC 412 ms
105,980 KB
testcase_35 AC 208 ms
79,716 KB
testcase_36 AC 203 ms
79,676 KB
testcase_37 AC 202 ms
79,660 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)
    count = 0
    for ind in range(n):
        if uf.find(ind) != ind:
            continue
        s = uf.size(ind)
        count += s*(s-1)//2
    
    ans += (count<<i)%mod
    ans %= mod
print(ans)
0