結果

問題 No.556 仁義なきサルたち
ユーザー 👑 obakyanobakyan
提出日時 2019-05-04 18:26:02
言語 Lua
(LuaJit 2.1.1696795921)
結果
WA  
実行時間 -
コード長 703 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 5,260 KB
実行使用メモリ 5,200 KB
最終ジャッジ日時 2023-09-05 08:20:48
合計ジャッジ時間 2,239 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

local ior = io.input()
local n, m = ior:read("*n", "*n")
local parent, gnum = {}, {}
for i = 1, n do parent[i], gnum[i] = i, 1 end

local function findroot(idx)
  while(parent[idx] ~= idx) do
    idx = parent[idx]
  end
  return idx
end

for i = 1, m do
  local a, b = ior:read("*n", "*n")
  local ar, br = findroot(a), findroot(b)
  parent[a], parent[b] = ar, br
  if(ar ~= br) then
    local an, bn = gnum[ar], gnum[br]
    local dst = true
    if(an < bn or (na == nb and br < ar)) then
      gnum[ar], gnum[br] = 0, gnum[ar] + gnum[br]
      parent[ar] = br
    else
      gnum[ar], gnum[br] = gnum[ar] + gnum[br], 0
      parent[br] = ar
    end
  end
end

for i = 1, n do
  print(findroot(i))
end
0