結果

問題 No.1390 Get together
ユーザー masashi0217masashi0217
提出日時 2021-03-25 07:20:12
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 1,231 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 11,488 KB
実行使用メモリ 31,300 KB
最終ジャッジ日時 2023-08-18 00:49:45
合計ジャッジ時間 15,675 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,348 KB
testcase_01 AC 82 ms
15,076 KB
testcase_02 AC 82 ms
15,108 KB
testcase_03 AC 95 ms
15,360 KB
testcase_04 AC 90 ms
15,288 KB
testcase_05 WA -
testcase_06 AC 88 ms
15,452 KB
testcase_07 WA -
testcase_08 AC 90 ms
15,352 KB
testcase_09 AC 91 ms
15,648 KB
testcase_10 AC 80 ms
15,012 KB
testcase_11 WA -
testcase_12 AC 80 ms
15,116 KB
testcase_13 WA -
testcase_14 AC 80 ms
15,216 KB
testcase_15 AC 80 ms
15,228 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 480 ms
21,880 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 728 ms
31,144 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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