結果

問題 No.1390 Get together
ユーザー gmm_teagmm_tea
提出日時 2021-02-12 21:51:03
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 638 bytes
コンパイル時間 552 ms
コンパイル使用メモリ 11,496 KB
実行使用メモリ 43,764 KB
最終ジャッジ日時 2023-09-27 03:42:26
合計ジャッジ時間 15,656 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,256 KB
testcase_01 AC 78 ms
15,352 KB
testcase_02 AC 80 ms
15,268 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 79 ms
15,132 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 82 ms
15,088 KB
testcase_16 AC 729 ms
42,740 KB
testcase_17 AC 660 ms
36,624 KB
testcase_18 AC 805 ms
43,764 KB
testcase_19 AC 763 ms
37,228 KB
testcase_20 AC 762 ms
38,112 KB
testcase_21 AC 773 ms
37,964 KB
testcase_22 AC 774 ms
40,736 KB
testcase_23 AC 771 ms
41,056 KB
testcase_24 AC 781 ms
41,344 KB
testcase_25 AC 766 ms
37,344 KB
testcase_26 AC 768 ms
37,376 KB
testcase_27 AC 765 ms
37,392 KB
testcase_28 AC 772 ms
37,324 KB
testcase_29 AC 766 ms
37,180 KB
testcase_30 AC 776 ms
37,080 KB
testcase_31 AC 766 ms
37,148 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 do |t|
      l[t]=min if l[t]!=min
    end
  end
end
c=[0]*n
m.times do |i|
  if l[i]!=i&&l[i]!=l[l[i]]
    l[i]=l[l[i]]
  end
  c[l[i]]+=1
end
ans=m
m.times do |i|
  if l[i]==i
    ans-=1
  end
end
puts ans
0