結果
問題 | No.1390 Get together |
ユーザー |
![]() |
提出日時 | 2021-02-12 22:43:48 |
言語 | Crystal (1.14.0) |
結果 |
AC
|
実行時間 | 154 ms / 2,000 ms |
コード長 | 930 bytes |
コンパイル時間 | 14,169 ms |
コンパイル使用メモリ | 295,992 KB |
実行使用メモリ | 36,480 KB |
最終ジャッジ日時 | 2024-07-19 23:39:29 |
合計ジャッジ時間 | 18,251 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
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 root?(x : Int32)@d[x] < 0endendn, m = read_line.split.map(&.to_i)a = (0...n).map { [] of Int32 }n.times dob, c = read_line.split.map(&.to_i.pred)a[c] << benduf = UnionFind.new(m)a.each do |b|b.sort.each_cons_pair { |i, j| uf.unite(i, j) }endputs (0...m).select { |i| uf.root?(i) }.sum { |i| uf.size(i) - 1 }