Submission #63548237


Source Code Expand

class WeightedUnionFind
  attr_accessor(:node, :val)

  def initialize(n)
    @node = Array.new(n, -1)
    @val = Array.new(n, 0)
  end

  def root(x)
    if @node[x] < 0
      return x
    else
      r = root(@node[x])
      @val[x] ^= @val[@node[x]]
      @node[x] = r
      return r
    end
  end

  def size(x)
    x = root(x)
    return (- @node[x])
  end

  def same?(x, y)
    return root(x) == root(y)
  end

  def weight(x)
    root(x)
    return @val[x]
  end

  def diff(x, y)
    return weight(y) ^ weight(x)
  end

  def unite(x, y, w)
    w ^= weight(x)
    w ^= weight(y)
    rx = root(x)
    ry = root(y)
    return if rx == ry
    
    dx = @node[rx]
    dy = @node[ry]
    if dx <= dy # xの方が大きい
      @node[ry] = rx
      @node[rx] += dy
      @val[ry] = w
    else
      @node[rx] = ry
      @node[ry] += dx
      @val[rx] = w
    end
  end
end

n, m = gets.chomp.split.map(&:to_i)
wuf = WeightedUnionFind.new(n)
m.times do
  xi, yi, zi = gets.chomp.split.map(&:to_i)
  xi -= 1
  yi -= 1
  if wuf.same?(xi, yi) && wuf.diff(xi, yi) != zi
    puts "-1"
    exit
  end

  wuf.unite(xi, yi, zi)
end

cnt = Hash.new
a = Array.new(n)
n.times do |v|
  r = wuf.root(v)
  if wuf.size(r) == 1
    a[v] = 0
    next
  end

  if cnt[r].nil?
    cnt[r] ||= Array.new(30) { Array.new(2, 0) }
  end

  w = wuf.diff(v, r)
  30.times do |d|
    cnt[r][d][(w >> d) & 1] += 1
  end
end

n.times do |v|
  r = wuf.root(v)
  next if wuf.size(r) == 1 || v != r

  a[r] = 0
  30.times do |d|
    if cnt[r][d][1] > cnt[r][d][0]
      bit = 1
    else
      bit = 0
    end
    a[v] |= (bit << d)
  end
end

n.times do |v|
  r = wuf.root(v)
  next if wuf.size(r) == 1 || v == r

  a[v] = wuf.diff(r, v) ^ a[r]
end

puts a.join(" ")

Submission Info

Submission Time
Task E - Min of Restricted Sum
User qib
Language Ruby (ruby 3.2.2)
Score 450
Code Size 1849 Byte
Status AC
Exec Time 1303 ms
Memory 185792 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 38
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 44 ms 17504 KiB
00_sample_01.txt AC 43 ms 17284 KiB
00_sample_02.txt AC 43 ms 17480 KiB
01_handmade_00.txt AC 42 ms 17172 KiB
01_handmade_01.txt AC 111 ms 22412 KiB
01_handmade_02.txt AC 42 ms 17228 KiB
01_handmade_03.txt AC 43 ms 20492 KiB
01_handmade_04.txt AC 42 ms 16788 KiB
01_handmade_05.txt AC 43 ms 20324 KiB
01_handmade_06.txt AC 110 ms 22620 KiB
01_handmade_07.txt AC 1294 ms 185792 KiB
01_handmade_08.txt AC 1303 ms 184840 KiB
01_handmade_09.txt AC 197 ms 17568 KiB
02_random_00.txt AC 333 ms 21088 KiB
02_random_01.txt AC 617 ms 76320 KiB
02_random_02.txt AC 577 ms 45576 KiB
02_random_03.txt AC 732 ms 70576 KiB
02_random_04.txt AC 294 ms 48096 KiB
02_random_05.txt AC 630 ms 74732 KiB
02_random_06.txt AC 415 ms 25200 KiB
02_random_07.txt AC 736 ms 67876 KiB
02_random_08.txt AC 444 ms 60716 KiB
02_random_09.txt AC 460 ms 66020 KiB
02_random_10.txt AC 49 ms 17528 KiB
02_random_11.txt AC 489 ms 66464 KiB
02_random_12.txt AC 58 ms 17768 KiB
02_random_13.txt AC 161 ms 20636 KiB
02_random_14.txt AC 81 ms 18340 KiB
02_random_15.txt AC 121 ms 20664 KiB
02_random_16.txt AC 52 ms 18012 KiB
02_random_17.txt AC 61 ms 20540 KiB
02_random_18.txt AC 388 ms 58352 KiB
02_random_19.txt AC 734 ms 71308 KiB
02_random_20.txt AC 115 ms 18044 KiB
02_random_21.txt AC 380 ms 20844 KiB
02_random_22.txt AC 253 ms 19320 KiB
02_random_23.txt AC 387 ms 20936 KiB
02_random_24.txt AC 193 ms 17588 KiB