結果

問題 No.1420 国勢調査 (Easy)
ユーザー convexineqconvexineq
提出日時 2021-06-20 03:46:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 2,021 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 87,016 KB
実行使用メモリ 80,676 KB
最終ジャッジ日時 2023-09-05 01:22:49
合計ジャッジ時間 10,907 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,332 KB
testcase_01 AC 70 ms
71,432 KB
testcase_02 AC 248 ms
78,468 KB
testcase_03 AC 247 ms
78,844 KB
testcase_04 AC 250 ms
79,068 KB
testcase_05 AC 248 ms
78,588 KB
testcase_06 AC 249 ms
78,500 KB
testcase_07 AC 173 ms
78,440 KB
testcase_08 AC 208 ms
78,244 KB
testcase_09 AC 178 ms
78,328 KB
testcase_10 AC 211 ms
78,516 KB
testcase_11 AC 168 ms
78,340 KB
testcase_12 AC 102 ms
78,828 KB
testcase_13 AC 140 ms
79,780 KB
testcase_14 AC 115 ms
79,236 KB
testcase_15 AC 142 ms
79,680 KB
testcase_16 AC 137 ms
79,720 KB
testcase_17 AC 136 ms
79,484 KB
testcase_18 AC 136 ms
79,696 KB
testcase_19 AC 136 ms
79,720 KB
testcase_20 AC 137 ms
79,796 KB
testcase_21 AC 138 ms
79,756 KB
testcase_22 AC 283 ms
80,264 KB
testcase_23 AC 278 ms
80,380 KB
testcase_24 AC 279 ms
80,600 KB
testcase_25 AC 287 ms
80,676 KB
testcase_26 AC 284 ms
79,904 KB
testcase_27 AC 174 ms
80,028 KB
testcase_28 AC 182 ms
79,632 KB
testcase_29 AC 146 ms
79,344 KB
testcase_30 AC 187 ms
79,956 KB
testcase_31 AC 170 ms
80,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class PotentialUnionFind_general:
    def __init__(self, n, add, inv, e_M):
        self.parent = [-1]*n #親ノード or size
        self.wt = [0]*n #親ノードを基準としたポテンシャル
        self.add = add #足し算
        self.inv = inv #逆元
        self.e_M = e_M #単位元

    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] >= 0:
            x = self.parent[x]
        return x 

    def weight(self, x): # p(x) - p(root)
        c = self.e_M
        while self.parent[x] >= 0:
            c = self.add(self.wt[x], c)
            x = self.parent[x]
        return c

    def merge(self, x, y, dxy): #ポテンシャル差 p(x)*dxy = p(y)でxとyの組をまとめる
        dxy = self.add(self.add(self.weight(x),dxy), inv(self.weight(y))) #dxyを置き換え
        x,y = self.root(x), self.root(y)
        if x == y: return False
        if self.parent[x] > self.parent[y]: #rxの要素数が大きいように
            x,y,dxy = y,x,inv(dxy)
        self.parent[x] += self.parent[y] #xの要素数を更新
        self.parent[y] = x #ryをrxにつなぐ
        self.wt[y] = dxy #ryの相対ポテンシャルを更新
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): #diff(x,y): xを基準としたyのポテンシャルを返す 
        return add(inv(self.weight(x)), self.weight(y))

    def size(self,x): #size(x): xのいるグループの要素数を返す
        return self.gsize[self.root(x)]

import sys
readline = sys.stdin.readline

n,m = map(int,readline().split())
add = lambda x,y: x^y
inv = lambda x: x
UF = PotentialUnionFind_general(n+1,add,inv,0)

for _ in range(m):
    l,r = map(int,readline().split())
    y = int(readline())
    if UF.issame(l,r):
        if UF.diff(l,r) != y:
            print(-1)
            exit()
    else:
        UF.merge(l,r,y)

for i in range(1,n+1):
    print(UF.weight(i))
0