結果
問題 |
No.2290 UnUnion Find
|
ユーザー |
👑 |
提出日時 | 2023-05-05 23:51:06 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,173 bytes |
コンパイル時間 | 29 ms |
コンパイル使用メモリ | 6,692 KB |
実行使用メモリ | 18,632 KB |
最終ジャッジ日時 | 2024-11-23 13:01:31 |
合計ジャッジ時間 | 56,651 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 4 WA * 27 TLE * 15 |
ソースコード
local UF = {} UF.create = function(self, n) self.parent = {} self.g = {} for i = 1, n do self.parent[i] = i self.g[i] = true end self.gc = n 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) if ra ~= rb then self.g[rb] = nil self.gc = self.gc - 1 self.parent[b], self.parent[rb] = ra, ra end end UF.new = function(n) local obj = {} setmetatable(obj, {__index = UF}) obj:create(n) return obj end local n = io.read("*n") local uf = UF.new(n) local q = io.read("*n") for iq = 1, q do local tp = io.read("*n") if tp == 1 then local a, b = io.read("*n", "*n") uf:unite(a, b) else local a = io.read("*n") if 1 < uf.gc then local g = uf.g local k1, v1 = next(g) local k2, v2 = next(g, k1) local r = uf:getroot(a) print(k1 + k2 - r) else print(-1) end end end