結果

問題 No.748 yuki国のお財布事情
ユーザー simansiman
提出日時 2022-04-25 05:26:10
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,056 ms / 2,000 ms
コード長 1,050 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 11,468 KB
実行使用メモリ 33,664 KB
最終ジャッジ日時 2023-09-09 07:15:39
合計ジャッジ時間 12,871 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,276 KB
testcase_01 AC 83 ms
15,116 KB
testcase_02 AC 84 ms
15,028 KB
testcase_03 AC 84 ms
15,184 KB
testcase_04 AC 83 ms
15,088 KB
testcase_05 AC 82 ms
15,128 KB
testcase_06 AC 82 ms
15,164 KB
testcase_07 AC 82 ms
15,168 KB
testcase_08 AC 82 ms
15,116 KB
testcase_09 AC 83 ms
15,280 KB
testcase_10 AC 82 ms
15,124 KB
testcase_11 AC 82 ms
15,236 KB
testcase_12 AC 82 ms
15,164 KB
testcase_13 AC 182 ms
17,200 KB
testcase_14 AC 251 ms
18,504 KB
testcase_15 AC 199 ms
17,552 KB
testcase_16 AC 415 ms
21,696 KB
testcase_17 AC 918 ms
30,792 KB
testcase_18 AC 1,005 ms
32,260 KB
testcase_19 AC 1,056 ms
33,536 KB
testcase_20 AC 934 ms
31,176 KB
testcase_21 AC 981 ms
32,320 KB
testcase_22 AC 81 ms
15,136 KB
testcase_23 AC 81 ms
15,136 KB
testcase_24 AC 82 ms
15,080 KB
testcase_25 AC 943 ms
31,252 KB
testcase_26 AC 1,013 ms
33,664 KB
testcase_27 AC 1,014 ms
33,532 KB
testcase_28 AC 703 ms
27,472 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, K = gets.split.map(&:to_i)
E = []

M.times do |i|
  a, b, c = gets.split.map(&:to_i)

  E << [a, b, c, 0]
end

uf = UnionFind.new(N + 1)

K.times do
  e = gets.to_i
  E[e - 1][-1] = -1
end

E.sort_by! { |a, b, c, d| [d, c] }
sum = E.map { |e| e[-2] }.sum
ans = 0

E.each do |a, b, c, d|
  if d == -1
    uf.unite(a, b)
    ans += c
  elsif !uf.same?(a, b)
    uf.unite(a, b)
    ans += c
  end
end

puts sum - ans
0