結果

問題 No.1420 国勢調査 (Easy)
ユーザー qibqib
提出日時 2023-01-23 00:32:24
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 1,311 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 839,536 KB
最終ジャッジ日時 2023-09-07 07:26:21
合計ジャッジ時間 5,563 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,316 KB
testcase_01 AC 75 ms
71,268 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 6)

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