結果
| 問題 | No.1420 国勢調査 (Easy) | 
| コンテスト | |
| ユーザー |  convexineq | 
| 提出日時 | 2021-06-20 03:46:56 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 243 ms / 2,000 ms | 
| コード長 | 2,021 bytes | 
| コンパイル時間 | 331 ms | 
| コンパイル使用メモリ | 82,560 KB | 
| 実行使用メモリ | 79,488 KB | 
| 最終ジャッジ日時 | 2024-06-22 22:26:21 | 
| 合計ジャッジ時間 | 7,292 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 30 | 
ソースコード
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))
            
            
            
        