結果

問題 No.1420 国勢調査 (Easy)
ユーザー qibqib
提出日時 2023-01-23 00:30:30
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,268 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 163,024 KB
最終ジャッジ日時 2023-09-07 07:25:25
合計ジャッジ時間 16,725 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,324 KB
testcase_01 AC 77 ms
71,116 KB
testcase_02 RE -
testcase_03 WA -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 AC 133 ms
77,760 KB
testcase_09 AC 134 ms
78,060 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 148 ms
79,792 KB
testcase_13 AC 192 ms
80,836 KB
testcase_14 AC 157 ms
79,496 KB
testcase_15 AC 190 ms
79,836 KB
testcase_16 AC 195 ms
80,260 KB
testcase_17 AC 195 ms
80,268 KB
testcase_18 AC 191 ms
80,400 KB
testcase_19 AC 190 ms
80,480 KB
testcase_20 AC 192 ms
80,296 KB
testcase_21 AC 188 ms
79,848 KB
testcase_22 WA -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 WA -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 187 ms
79,776 KB
testcase_31 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class WeightedUnionFind:
  def __init__(self, n):
    self.node = [-1 for _ in range(n)]
    self.value = [0 for _ in range(n)]

  def root(self, x):
    if self.node[x] < 0:
      return x
    else:
      r = self.root(self.node[x])
      self.value[x] ^= self.value[self.node[x]]
      self.node[x] = r
      return r

  def size(self, x):
    x = self.root(x)
    return (- self.node[x])

  def is_same(self, x, y):
    return self.root(x) == self.root(y)

  def weight(self, x):
    self.root(x)
    return self.value[x]

  def diff(self, x, y):
    return self.weight(y) ^ self.weight(x)

  def unite(self, x, y, w):
    w ^= self.weight(x)
    w ^= self.weight(y)
    rx = self.root(x)
    ry = self.root(y)
    if rx == ry:
      return

    dx = self.node[x]
    dy = self.node[y]
    if dx <= dy:
      self.node[ry] = rx
      self.node[rx] += dy
      self.value[ry] = w
    else:
      self.node[rx] = ry
      self.node[ry] += dx
      self.value[rx] = w

n, m = map(int, input().split())
wuf = WeightedUnionFind(n)
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  y = int(input())
  if not wuf.is_same(a, b):
    wuf.unite(a, b, y)
  elif wuf.diff(a, b) != y:
    print("-1")
    exit()

for v in range(n):
  print(wuf.weight(v))
0