結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 obakyanobakyan
提出日時 2023-08-12 13:42:45
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 959 bytes
コンパイル時間 177 ms
コンパイル使用メモリ 5,460 KB
実行使用メモリ 6,588 KB
最終ジャッジ日時 2023-08-12 13:46:21
合計ジャッジ時間 2,269 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 65 ms
6,488 KB
testcase_04 AC 14 ms
6,164 KB
testcase_05 AC 44 ms
4,380 KB
testcase_06 AC 42 ms
6,432 KB
testcase_07 AC 48 ms
4,384 KB
testcase_08 AC 39 ms
6,468 KB
testcase_09 AC 60 ms
4,468 KB
testcase_10 AC 67 ms
6,588 KB
testcase_11 AC 57 ms
6,508 KB
testcase_12 AC 36 ms
4,384 KB
testcase_13 AC 19 ms
4,376 KB
testcase_14 AC 34 ms
4,380 KB
testcase_15 AC 36 ms
4,380 KB
testcase_16 AC 72 ms
4,768 KB
testcase_17 AC 22 ms
4,384 KB
testcase_18 AC 33 ms
6,584 KB
testcase_19 AC 9 ms
4,384 KB
testcase_20 AC 43 ms
4,376 KB
testcase_21 AC 21 ms
4,376 KB
testcase_22 AC 25 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local UF = {}
UF.create = function(self, n)
  self.parent = {}
  for i = 1, n do
    self.parent[i] = i
  end
end
UF.getroot = function(self, idx)
  local parent = self.parent
  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
UF.unite = function(self, a, b)
  local ra = self:getroot(a)
  local rb = self:getroot(b)
  self.parent[b], self.parent[rb] = ra, ra
end
UF.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = UF})
  obj:create(n)
  return obj
end

local n, m = io.read("*n", "*n")
local uf = UF.new(2 * n)
for i = 1, m do
  local a, b = io.read("*n", "*n")
  uf:unite(a, b)
end
local g = {}
for i = 1, 2 * n do
  local r = uf:getroot(i)
  if not g[r] then g[r] = 0 end
  g[r] = g[r] + 1
end
local ans = n
for key, val in pairs(g) do
  ans = ans - math.floor(val / 2)
end
print(ans)
0