結果

問題 No.945 YKC饅頭
ユーザー simansiman
提出日時 2022-11-30 18:25:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,554 ms / 2,000 ms
コード長 1,072 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 24,320 KB
最終ジャッジ日時 2024-04-16 16:04:25
合計ジャッジ時間 35,437 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
12,288 KB
testcase_01 AC 90 ms
12,160 KB
testcase_02 AC 91 ms
12,160 KB
testcase_03 AC 95 ms
12,160 KB
testcase_04 AC 91 ms
12,160 KB
testcase_05 AC 93 ms
12,288 KB
testcase_06 AC 96 ms
12,288 KB
testcase_07 AC 95 ms
12,288 KB
testcase_08 AC 91 ms
12,288 KB
testcase_09 AC 93 ms
12,160 KB
testcase_10 AC 94 ms
12,288 KB
testcase_11 AC 91 ms
12,288 KB
testcase_12 AC 92 ms
12,160 KB
testcase_13 AC 96 ms
12,160 KB
testcase_14 AC 95 ms
12,288 KB
testcase_15 AC 89 ms
12,160 KB
testcase_16 AC 93 ms
12,160 KB
testcase_17 AC 95 ms
12,288 KB
testcase_18 AC 91 ms
12,160 KB
testcase_19 AC 89 ms
12,288 KB
testcase_20 AC 88 ms
12,160 KB
testcase_21 AC 89 ms
12,160 KB
testcase_22 AC 93 ms
12,160 KB
testcase_23 AC 96 ms
12,160 KB
testcase_24 AC 95 ms
12,416 KB
testcase_25 AC 95 ms
12,288 KB
testcase_26 AC 102 ms
12,288 KB
testcase_27 AC 92 ms
12,160 KB
testcase_28 AC 93 ms
12,160 KB
testcase_29 AC 89 ms
12,160 KB
testcase_30 AC 89 ms
12,288 KB
testcase_31 AC 200 ms
13,568 KB
testcase_32 AC 456 ms
16,896 KB
testcase_33 AC 734 ms
17,664 KB
testcase_34 AC 570 ms
16,128 KB
testcase_35 AC 1,282 ms
20,096 KB
testcase_36 AC 419 ms
12,544 KB
testcase_37 AC 398 ms
13,056 KB
testcase_38 AC 465 ms
13,696 KB
testcase_39 AC 442 ms
16,000 KB
testcase_40 AC 714 ms
18,048 KB
testcase_41 AC 364 ms
16,640 KB
testcase_42 AC 702 ms
16,256 KB
testcase_43 AC 375 ms
12,416 KB
testcase_44 AC 880 ms
17,920 KB
testcase_45 AC 679 ms
14,848 KB
testcase_46 AC 247 ms
14,720 KB
testcase_47 AC 747 ms
16,640 KB
testcase_48 AC 192 ms
12,672 KB
testcase_49 AC 535 ms
16,256 KB
testcase_50 AC 806 ms
15,872 KB
testcase_51 AC 1,517 ms
20,992 KB
testcase_52 AC 1,383 ms
20,864 KB
testcase_53 AC 1,055 ms
20,864 KB
testcase_54 AC 1,306 ms
20,864 KB
testcase_55 AC 1,554 ms
20,864 KB
testcase_56 AC 221 ms
20,480 KB
testcase_57 AC 160 ms
20,608 KB
testcase_58 AC 865 ms
24,064 KB
testcase_59 AC 1,038 ms
24,064 KB
testcase_60 AC 483 ms
23,168 KB
testcase_61 AC 929 ms
24,064 KB
testcase_62 AC 893 ms
24,192 KB
testcase_63 AC 157 ms
20,992 KB
testcase_64 AC 502 ms
23,040 KB
testcase_65 AC 428 ms
22,528 KB
testcase_66 AC 406 ms
22,400 KB
testcase_67 AC 696 ms
23,168 KB
testcase_68 AC 486 ms
23,168 KB
testcase_69 AC 252 ms
21,760 KB
testcase_70 AC 282 ms
21,888 KB
testcase_71 AC 283 ms
21,632 KB
testcase_72 AC 579 ms
23,296 KB
testcase_73 AC 975 ms
24,320 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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']]
0