結果

問題 No.1390 Get together
ユーザー gmm_teagmm_tea
提出日時 2021-02-12 23:27:36
言語 Ruby
(3.3.0)
結果
AC  
実行時間 716 ms / 2,000 ms
コード長 622 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 11,520 KB
実行使用メモリ 43,612 KB
最終ジャッジ日時 2023-09-27 07:01:01
合計ジャッジ時間 14,956 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
15,244 KB
testcase_01 AC 74 ms
15,172 KB
testcase_02 AC 72 ms
15,080 KB
testcase_03 AC 83 ms
15,452 KB
testcase_04 AC 81 ms
15,496 KB
testcase_05 AC 82 ms
15,492 KB
testcase_06 AC 80 ms
15,532 KB
testcase_07 AC 82 ms
15,404 KB
testcase_08 AC 83 ms
15,320 KB
testcase_09 AC 87 ms
15,716 KB
testcase_10 AC 74 ms
15,212 KB
testcase_11 AC 73 ms
15,148 KB
testcase_12 AC 71 ms
15,108 KB
testcase_13 AC 71 ms
15,124 KB
testcase_14 AC 71 ms
15,072 KB
testcase_15 AC 73 ms
15,108 KB
testcase_16 AC 647 ms
41,152 KB
testcase_17 AC 583 ms
34,932 KB
testcase_18 AC 716 ms
43,612 KB
testcase_19 AC 663 ms
35,872 KB
testcase_20 AC 666 ms
36,780 KB
testcase_21 AC 681 ms
36,960 KB
testcase_22 AC 699 ms
39,116 KB
testcase_23 AC 693 ms
40,432 KB
testcase_24 AC 687 ms
40,688 KB
testcase_25 AC 662 ms
35,580 KB
testcase_26 AC 664 ms
35,612 KB
testcase_27 AC 676 ms
35,956 KB
testcase_28 AC 678 ms
35,744 KB
testcase_29 AC 668 ms
35,464 KB
testcase_30 AC 644 ms
35,620 KB
testcase_31 AC 649 ms
35,680 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n,m=gets.split.map(&:to_i)
lc=[]
n.times do
  lc << []
end
n.times do
  b,c=gets.split.map(&:to_i).map{|x|x-1}
  lc[c] << b
end

l=[]
m.times do |i|
  l << i
end
lc.each do |lcc|
  a=0
  lcc.uniq.each.with_index do |ll,i|
    if i==0
      a=ll
      next
    end
    b=ll
    s=[a,b]
    k=a
    while l[k]!=k
      k=l[k]
      s << k
    end
    k=b
    while l[k]!=k
      k=l[k]
      s << k
    end
    min=s.min
    s.uniq.each do |t|
      l[t]=min if l[t]!=min
    end
  end
end
m.times do |i|
  if l[i]!=i&&l[i]!=l[l[i]]
    l[i]=l[l[i]]
  end
end
ans=m
m.times do |i|
  if l[i]==i
    ans-=1
  end
end
puts ans
0