結果
問題 | No.74 貯金箱の退屈 |
ユーザー |
![]() |
提出日時 | 2021-02-23 10:08:27 |
言語 | Crystal (1.14.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,153 bytes |
コンパイル時間 | 15,419 ms |
コンパイル使用メモリ | 297,440 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-22 06:14:34 |
合計ジャッジ時間 | 15,144 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
lib Cfun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64endclass Stringdef to_i64C.strtoll(self, nil, 10)endendclass UnionFind@d : Array(Int32)def initialize(@n : Int32)@d = Array.new(n, -1)enddef root(x : Int32)@d[x] < 0 ? x : (@d[x] = root(@d[x]))enddef unite(x : Int32, y : Int32)x = root(x)y = root(y)return false if x == yx, y = y, x if @d[x] > @d[y]@d[x] += @d[y]@d[y] = xtrueenddef same?(x : Int32, y : Int32)root(x) == root(y)enddef size(x : Int32)-@d[root(x)]enddef groupsgroups = Hash(Int32, Set(Int32)).new { |h, k| h[k] = Set(Int32).new }@n.times do |i|groups[root(i)] << iendgroups.values.to_setendendn = read_line.to_ia = read_line.split.map(&.to_i)b = read_line.split.map(&.to_i)flag = [false] * nuf = UnionFind.new(n)n.times do |i|v1 = (i + a[i]) % nv2 = (i - a[i]) % nif v1 == v2flag[v1] = trueelseuf.unite(v1, v2)endendputs uf.groups.all? { |set|f1 = set.any? { |i| flag[i] }f2 = set.count { |i| b[i] == 0 }.even?f1 || f2} ? "Yes" : "No"