結果
問題 |
No.945 YKC饅頭
|
ユーザー |
![]() |
提出日時 | 2022-11-30 18:25:36 |
言語 | Ruby (3.4.1) |
結果 |
AC
|
実行時間 | 1,399 ms / 2,000 ms |
コード長 | 1,072 bytes |
コンパイル時間 | 302 ms |
コンパイル使用メモリ | 7,552 KB |
実行使用メモリ | 24,448 KB |
最終ジャッジ日時 | 2024-10-07 13:13:00 |
合計ジャッジ時間 | 31,260 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 74 |
コンパイルメッセージ
Syntax OK
ソースコード
class UnionFind def initialize(n) @size = Array.new(n, 1) @rank = Array.new(n, 0) @parent = [] (0..n).each do |i| @parent[i] = i end end def find(x) if @parent[x] == x x else @parent[x] = find(@parent[x]) end end def unite(x, y) x = find(x) y = find(y) return if x == y if @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] end end def same?(x, y) find(x) == find(y) end def size(x) @size[find(x)] end end N, M = gets.split.map(&:to_i) uf = UnionFind.new(N + 1) memo = Array.new(N + 1) G = Array.new(N + 1, -1) M.times do l, r, t = gets.chomp.split l = l.to_i r = r.to_i from = uf.find(l) to = uf.find(r) from.upto(to) do |idx| uf.unite(from, idx) memo[idx] = t if memo[idx].nil? end end counter = Hash.new(0) 1.upto(N) do |idx| counter[memo[idx]] += 1 end puts "%d %d %d" % [counter['Y'], counter['K'], counter['C']]