結果

問題 No.1390 Get together
ユーザー 👑 obakyanobakyan
提出日時 2021-05-24 08:29:49
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 764 bytes
コンパイル時間 28 ms
コンパイル使用メモリ 5,120 KB
実行使用メモリ 25,216 KB
最終ジャッジ日時 2024-04-20 18:26:33
合計ジャッジ時間 4,527 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 126 ms
22,912 KB
testcase_17 AC 170 ms
25,088 KB
testcase_18 AC 121 ms
22,912 KB
testcase_19 AC 180 ms
25,216 KB
testcase_20 AC 182 ms
25,216 KB
testcase_21 AC 193 ms
25,216 KB
testcase_22 AC 152 ms
24,448 KB
testcase_23 AC 158 ms
24,448 KB
testcase_24 AC 148 ms
24,448 KB
testcase_25 AC 175 ms
25,216 KB
testcase_26 AC 166 ms
25,216 KB
testcase_27 AC 170 ms
25,216 KB
testcase_28 AC 171 ms
25,216 KB
testcase_29 AC 166 ms
25,216 KB
testcase_30 AC 169 ms
25,216 KB
testcase_31 AC 178 ms
25,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, m = io.read("*n", "*n")
local col = {}

for i = 1, n do
  col[i] = {}
end
for i = 1, n do
  local b, c = io.read("*n", "*n")
  table.insert(col[c], b)
end

local parent = {}
for i = 1, m do
  parent[i] = i
end

local function uf_findroot(idx)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end
local ret = 0
for icol = 1, n do
  if 1 < #col[icol] then
    local r = uf_findroot(col[icol][1])
    for i = 2, #col[icol] do
      local b = col[icol][i]
      local rb = uf_findroot(b)
      if rb ~= r then
        ret = ret + 1
        parent[rb], parent[b] = r, r
      end
    end
  end
end
print(ret)
0