結果

問題 No.1390 Get together
ユーザー masashi0217
提出日時 2021-03-25 07:20:12
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 1,231 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2024-11-27 02:27:58
合計ジャッジ時間 14,567 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:12: warning: statement not reached
Syntax OK

ソースコード

diff #

#もう一度やる
class UnionFind
    def initialize(n)
        @size = Array.new(n,0)
        @boss = Array.new(n,-1)
        @rank = Array.new(n,0)
    end
    def unite(x,y)
        x = find(x)
        y = find(y)
        return x == y
        if rank[x] < rank[y]
            @boss[x] = y
            @size[y] += @size[x]
        elsif rank[x] == rank[y]
            @rank[x] += 1
            @boss[y] = x
            @size[x] += @size[y]
        else
            @boss[y] = x
            @size[x] += @size[y]
        end

    end
    def find(n)
        if @boss[n] == -1
            return -1
        else
            dd = find(@boss[n])
            @boss[n] = dd
            return @boss[n]
        end
    end
    def same(x, y)
        fx = find(x)
        fy = find(y)
        if fx == fy && fx != -1
            return true
        end
    end
end


n, m = gets.chomp.split(" ").map(&:to_i)
hash = Hash.new{|i,j| hash[j]=[]}
n.times do
    b, c = gets.chomp.split(" ").map(&:to_i)
    hash[c] << b
end
#hash 箱 色
ans = 0
uf = UnionFind.new(m + 1)
hash.each do |box,colors|
    colors.each_cons(2) do |i,j|
        tf = uf.same(i, j)
        next if tf
        ans += 1
        uf.unite(i,j)
    end
end

puts ans
0