結果

問題 No.1640 簡単な色塗り
ユーザー 👑 obakyanobakyan
提出日時 2021-11-23 22:53:51
言語 Lua
(LuaJit 2.1.1696795921)
結果
RE  
実行時間 -
コード長 2,164 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 5,296 KB
実行使用メモリ 41,008 KB
最終ジャッジ日時 2023-09-08 03:29:10
合計ジャッジ時間 18,988 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 AC 266 ms
39,824 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 RE -
testcase_09 AC 1 ms
4,380 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 339 ms
39,760 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 270 ms
32,736 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 155 ms
20,984 KB
testcase_22 AC 13 ms
4,380 KB
testcase_23 AC 221 ms
26,072 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 249 ms
30,800 KB
testcase_27 AC 91 ms
13,756 KB
testcase_28 AC 332 ms
38,016 KB
testcase_29 AC 241 ms
29,576 KB
testcase_30 AC 21 ms
6,344 KB
testcase_31 AC 166 ms
29,188 KB
testcase_32 AC 141 ms
26,592 KB
testcase_33 AC 89 ms
17,348 KB
testcase_34 AC 121 ms
24,240 KB
testcase_35 AC 92 ms
17,332 KB
testcase_36 AC 19 ms
6,356 KB
testcase_37 AC 28 ms
8,288 KB
testcase_38 AC 152 ms
26,956 KB
testcase_39 AC 55 ms
13,432 KB
testcase_40 AC 59 ms
13,808 KB
testcase_41 AC 124 ms
24,080 KB
testcase_42 AC 68 ms
14,496 KB
testcase_43 AC 70 ms
15,020 KB
testcase_44 AC 68 ms
14,484 KB
testcase_45 AC 54 ms
12,492 KB
testcase_46 AC 24 ms
7,388 KB
testcase_47 AC 14 ms
5,472 KB
testcase_48 AC 149 ms
27,428 KB
testcase_49 AC 8 ms
4,384 KB
testcase_50 AC 1 ms
4,384 KB
testcase_51 AC 1 ms
4,384 KB
testcase_52 AC 351 ms
41,008 KB
testcase_53 AC 341 ms
40,932 KB
07_evil_01.txt AC 912 ms
84,044 KB
07_evil_02.txt AC 1,468 ms
129,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n = io.read("*n")

local parent = {}
for i = 1, n 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 as, bs = {}, {}
for i = 1, n do
  local a, b = io.read("*n", "*n")
  as[i], bs[i] = a, b
  local ra, rb = uf_findroot(a), uf_findroot(b)
  parent[b], parent[rb] = ra, ra
end

local group = {}
local g_e = {}
local edge = {}
local edgecnt = {}
for i = 1, n do
  group[i] = {}
  g_e[i] = {}
  edge[i] = {}
  edgecnt[i] = 0
end
for i = 1, n do
  local r = uf_findroot(i)
  table.insert(group[r], i)
end
for i = 1, n do
  local a, b = as[i], bs[i]
  local r = uf_findroot(a)
  table.insert(g_e[r], i)
  edge[a][i], edge[b][i] = true, true
  edgecnt[a] = edgecnt[a] + 1
  edgecnt[b] = edgecnt[b] + 1
end
for i = 1, n do
  if #group[i] ~= #g_e[i] then print("No") os.exit() end
end
print("Yes")
local ans = {}
for i = 1, n do
  ans[i] = 0
end

local function solve(gi)
  local tasks = {}
  local done = 0
  for i = 1, #group[gi] do
    local src = group[gi][i]
    if edgecnt[src] == 1 then
      table.insert(tasks, src)
    end
  end
  while done < #tasks do
    done = done + 1
    local src = tasks[done]
    local ei = next(edge[src])
    ans[ei] = src
    local dst = as[ei] == src and bs[ei] or as[ei]
    edge[dst][ei] = nil
    edgecnt[dst] = edgecnt[dst] - 1
    if edgecnt[dst] == 1 then
      table.insert(tasks, dst)
    end
  end
  if done == #group[gi] then return end
  local ring_spos = false
  for i = 1, #group[gi] do
    local src = group[gi][i]
    if edgecnt[src] == 2 then
      ring_spos = src
      table.insert(tasks, src)
      break
    end
  end
  while done < #tasks do
    done = done + 1
    local src = tasks[done]
    local ei = next(edge[src])
    ans[ei] = src
    local dst = as[ei] == src and bs[ei] or as[ei]
    edge[dst][ei] = nil
    edgecnt[dst] = edgecnt[dst] - 1
    if edgecnt[dst] == 1 then
      table.insert(tasks, dst)
    end
  end
end

for i = 1, n do
  solve(i)
end
print(table.concat(ans, "\n"))
0