結果

問題 No.74 貯金箱の退屈
ユーザー yuruhiyayuruhiya
提出日時 2021-02-23 10:08:27
言語 Crystal
(1.11.2)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,153 bytes
コンパイル時間 20,651 ms
コンパイル使用メモリ 285,780 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-22 04:58:03
合計ジャッジ時間 21,865 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

class UnionFind
  @d : Array(Int32)

  def initialize(@n : Int32)
    @d = Array.new(n, -1)
  end

  def root(x : Int32)
    @d[x] < 0 ? x : (@d[x] = root(@d[x]))
  end

  def unite(x : Int32, y : Int32)
    x = root(x)
    y = root(y)
    return false if x == y
    x, y = y, x if @d[x] > @d[y]
    @d[x] += @d[y]
    @d[y] = x
    true
  end

  def same?(x : Int32, y : Int32)
    root(x) == root(y)
  end

  def size(x : Int32)
    -@d[root(x)]
  end

  def groups
    groups = Hash(Int32, Set(Int32)).new { |h, k| h[k] = Set(Int32).new }
    @n.times do |i|
      groups[root(i)] << i
    end
    groups.values.to_set
  end
end

n = read_line.to_i
a = read_line.split.map(&.to_i)
b = read_line.split.map(&.to_i)

flag = [false] * n
uf = UnionFind.new(n)
n.times do |i|
  v1 = (i + a[i]) % n
  v2 = (i - a[i]) % n
  if v1 == v2
    flag[v1] = true
  else
    uf.unite(v1, v2)
  end
end
puts uf.groups.all? { |set|
  f1 = set.any? { |i| flag[i] }
  f2 = set.count { |i| b[i] == 0 }.even?
  f1 || f2
} ? "Yes" : "No"
0