結果

問題 No.1390 Get together
ユーザー gmm_teagmm_tea
提出日時 2021-02-12 22:06:15
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 643 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 11,304 KB
実行使用メモリ 43,684 KB
最終ジャッジ日時 2023-09-27 04:10:14
合計ジャッジ時間 16,320 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,100 KB
testcase_01 AC 78 ms
15,056 KB
testcase_02 AC 76 ms
15,296 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 76 ms
15,132 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 77 ms
15,148 KB
testcase_16 AC 715 ms
42,612 KB
testcase_17 AC 633 ms
36,760 KB
testcase_18 AC 787 ms
43,684 KB
testcase_19 AC 736 ms
37,612 KB
testcase_20 AC 735 ms
38,340 KB
testcase_21 AC 748 ms
38,604 KB
testcase_22 AC 749 ms
40,860 KB
testcase_23 AC 753 ms
41,064 KB
testcase_24 AC 755 ms
41,520 KB
testcase_25 AC 737 ms
37,036 KB
testcase_26 AC 736 ms
37,028 KB
testcase_27 AC 733 ms
37,028 KB
testcase_28 AC 734 ms
37,156 KB
testcase_29 AC 728 ms
37,360 KB
testcase_30 AC 738 ms
37,424 KB
testcase_31 AC 740 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.each 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