結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 obakyanobakyan
提出日時 2023-08-12 13:42:45
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 959 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 5,248 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-30 03:53:36
合計ジャッジ時間 1,806 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 60 ms
7,168 KB
testcase_04 AC 12 ms
6,784 KB
testcase_05 AC 42 ms
5,376 KB
testcase_06 AC 38 ms
6,912 KB
testcase_07 AC 44 ms
5,376 KB
testcase_08 AC 36 ms
6,912 KB
testcase_09 AC 57 ms
5,376 KB
testcase_10 AC 60 ms
7,040 KB
testcase_11 AC 50 ms
7,296 KB
testcase_12 AC 34 ms
5,376 KB
testcase_13 AC 19 ms
5,376 KB
testcase_14 AC 33 ms
5,376 KB
testcase_15 AC 34 ms
5,376 KB
testcase_16 AC 66 ms
5,376 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 30 ms
6,912 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 39 ms
5,376 KB
testcase_21 AC 20 ms
5,376 KB
testcase_22 AC 24 ms
5,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