結果

問題 No.1639 最小通信路
ユーザー simansiman
提出日時 2021-09-21 16:03:19
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 908 bytes
コンパイル時間 48 ms
コンパイル使用メモリ 11,348 KB
実行使用メモリ 15,968 KB
最終ジャッジ日時 2023-09-17 02:13:13
合計ジャッジ時間 4,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
15,224 KB
testcase_01 AC 70 ms
15,284 KB
testcase_02 AC 75 ms
15,296 KB
testcase_03 AC 87 ms
15,968 KB
testcase_04 AC 87 ms
15,932 KB
testcase_05 AC 71 ms
15,304 KB
testcase_06 AC 69 ms
15,356 KB
testcase_07 AC 65 ms
15,008 KB
testcase_08 AC 73 ms
15,372 KB
testcase_09 AC 66 ms
15,104 KB
testcase_10 AC 67 ms
15,204 KB
testcase_11 AC 71 ms
15,388 KB
testcase_12 AC 67 ms
15,120 KB
testcase_13 AC 74 ms
15,432 KB
testcase_14 AC 65 ms
15,120 KB
testcase_15 AC 67 ms
15,076 KB
testcase_16 AC 70 ms
15,248 KB
testcase_17 AC 73 ms
15,308 KB
testcase_18 AC 66 ms
15,308 KB
testcase_19 AC 67 ms
15,060 KB
testcase_20 AC 73 ms
15,256 KB
testcase_21 AC 64 ms
15,016 KB
testcase_22 AC 68 ms
15,100 KB
testcase_23 AC 70 ms
15,144 KB
testcase_24 AC 67 ms
15,212 KB
testcase_25 AC 67 ms
15,160 KB
testcase_26 AC 66 ms
15,288 KB
testcase_27 AC 65 ms
15,236 KB
testcase_28 AC 64 ms
15,232 KB
testcase_29 AC 70 ms
15,592 KB
testcase_30 AC 75 ms
15,580 KB
testcase_31 AC 66 ms
15,080 KB
testcase_32 AC 73 ms
15,568 KB
testcase_33 AC 67 ms
15,296 KB
testcase_34 AC 72 ms
15,500 KB
testcase_35 AC 80 ms
15,816 KB
testcase_36 AC 71 ms
15,376 KB
testcase_37 AC 74 ms
15,536 KB
testcase_38 AC 65 ms
15,124 KB
testcase_39 AC 67 ms
15,120 KB
testcase_40 AC 65 ms
15,100 KB
testcase_41 AC 66 ms
15,260 KB
testcase_42 AC 74 ms
15,780 KB
testcase_43 AC 70 ms
15,208 KB
testcase_44 AC 69 ms
15,040 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 = gets.to_i
E = []

(N * (N - 1) / 2).times do
  a, b, c = gets.split.map(&:to_i)

  E << [a, b, c]
end

E.sort_by! { |a, b, c| c }

uf = UnionFind.new(N + 1)
max_r = -Float::INFINITY

E.each do |a, b, c|
  next if uf.same?(a, b)

  uf.unite(a, b)
  max_r = c
end

puts max_r
0