結果

問題 No.1420 国勢調査 (Easy)
ユーザー convexineqconvexineq
提出日時 2021-06-20 03:28:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,969 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 84,652 KB
最終ジャッジ日時 2023-09-05 01:20:33
合計ジャッジ時間 10,565 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
71,356 KB
testcase_01 AC 63 ms
71,316 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 154 ms
78,576 KB
testcase_08 AC 210 ms
78,876 KB
testcase_09 AC 164 ms
78,452 KB
testcase_10 AC 203 ms
78,148 KB
testcase_11 AC 138 ms
78,320 KB
testcase_12 AC 89 ms
78,208 KB
testcase_13 WA -
testcase_14 AC 101 ms
78,808 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 158 ms
79,152 KB
testcase_28 AC 156 ms
79,216 KB
testcase_29 AC 132 ms
79,468 KB
testcase_30 AC 159 ms
79,524 KB
testcase_31 AC 157 ms
79,800 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(y)-p(x)=dxyで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 not UF.merge(l,r,y):
        if UF.diff(l,r) != y:
            print(-1)
            exit()
print(*UF.wt[1:], sep="\n")
0