結果

問題 No.1843 Tree ANDistance
ユーザー ygd.ygd.
提出日時 2022-02-18 21:45:16
言語 PyPy3
(7.3.13)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,563 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 86,916 KB
実行使用メモリ 220,124 KB
最終ジャッジ日時 2023-09-11 18:52:09
合計ジャッジ時間 47,447 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 AC 1,998 ms
188,012 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 AC 1,620 ms
219,188 KB
testcase_06 AC 1,934 ms
220,124 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 1,314 ms
204,800 KB
testcase_13 AC 1,964 ms
198,004 KB
testcase_14 AC 160 ms
78,412 KB
testcase_15 AC 159 ms
78,292 KB
testcase_16 AC 142 ms
77,844 KB
testcase_17 AC 137 ms
77,816 KB
testcase_18 AC 126 ms
77,304 KB
testcase_19 AC 153 ms
78,224 KB
testcase_20 AC 134 ms
78,064 KB
testcase_21 AC 77 ms
71,112 KB
testcase_22 AC 78 ms
71,316 KB
testcase_23 AC 76 ms
71,408 KB
testcase_24 AC 76 ms
70,884 KB
testcase_25 AC 77 ms
71,216 KB
testcase_26 AC 77 ms
71,304 KB
testcase_27 AC 76 ms
71,044 KB
testcase_28 AC 1,502 ms
178,520 KB
testcase_29 AC 1,249 ms
156,492 KB
testcase_30 AC 1,262 ms
154,404 KB
testcase_31 AC 1,799 ms
197,116 KB
testcase_32 AC 1,752 ms
183,096 KB
testcase_33 AC 513 ms
114,204 KB
testcase_34 AC 1,196 ms
176,628 KB
testcase_35 AC 77 ms
71,112 KB
testcase_36 AC 78 ms
71,156 KB
testcase_37 AC 78 ms
71,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline #文字列につけてはダメ
input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
MOD = pow(10,9) + 7
#MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a,b,c = map(int,input().split())
        a -= 1; b -= 1
        G[a].append((b,c)); G[b].append((a,c))
    
    ans = 0
    for i in range(33):
        base = 1<<i #この桁を見ている。
        uf = UnionFind(N)
        stack = []
        stack.append(0)
        par = [-1]*N
        while stack:
            v = stack.pop()
            for u,c in G[v]:
                if par[v] == u: continue
                par[u] = v
                if (c>>i)&1 == 1:
                    uf.union(u,v)
                stack.append(u)
        used = set([])
        for j in range(N):
            if uf.find(j) in used: continue
            used.add(uf.find(j))
            #print(j,uf.find(j),uf.get_size(j))
            num = uf.get_size(j)
            ans += base*num*(num-1)//2
            ans %= MOD
    print(ans)


        
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]


if __name__ == '__main__':
    main()
0