結果
問題 | No.1390 Get together |
ユーザー |
![]() |
提出日時 | 2021-02-16 04:20:26 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 696 ms / 2,000 ms |
コード長 | 945 bytes |
コンパイル時間 | 123 ms |
コンパイル使用メモリ | 7,296 KB |
実行使用メモリ | 42,368 KB |
最終ジャッジ日時 | 2024-07-23 19:17:24 |
合計ジャッジ時間 | 14,148 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
コンパイルメッセージ
Syntax OK
ソースコード
class UnionFinddef initialize(n)@size = Array.new(n, 1)@rank = Array.new(n, 0)@parent = [](0..n).each do |i|@parent[i] = iendenddef find(x)if @parent[x] == xxelse@parent[x] = find(@parent[x])endenddef unite(x, y)x = find(x)y = find(y)return if x == yif @rank[x] < @rank[y]@parent[x] = y@size[y] += @size[x]else@parent[y] = x@size[x] += @size[y]@rank[x] += 1 if @rank[x] == @rank[y]endenddef same?(x, y)find(x) == find(y)enddef size(x)@size[find(x)]endendN, M = gets.split.map(&:to_i)Q = N.times.map { gets.split.map(&:to_i) }C = Hash.new { |h, k| h[k] = [] }Q.each do |b, c|C[c] << bendans = 0uf = UnionFind.new(M + 1)C.each do |c, pos_list|pos_list.each_cons(2) do |a, b|next if uf.same?(a, b)ans += 1uf.unite(a, b)endendputs ans