結果
| 問題 |
No.748 yuki国のお財布事情
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-04-25 05:26:10 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 1,042 ms / 2,000 ms |
| コード長 | 1,050 bytes |
| コンパイル時間 | 353 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 30,976 KB |
| 最終ジャッジ日時 | 2024-06-27 00:23:45 |
| 合計ジャッジ時間 | 13,005 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
コンパイルメッセージ
Syntax OK
ソースコード
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
siman