結果
| 問題 | 
                            No.1640 簡単な色塗り
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  | 
                    
| 提出日時 | 2021-11-23 22:53:51 | 
| 言語 | Lua  (LuaJit 2.1.1734355927)  | 
                    
| 結果 | 
                             
                                RE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,164 bytes | 
| コンパイル時間 | 249 ms | 
| コンパイル使用メモリ | 6,944 KB | 
| 実行使用メモリ | 48,896 KB | 
| 最終ジャッジ日時 | 2024-06-25 20:39:25 | 
| 合計ジャッジ時間 | 16,968 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 39 RE * 14 | 
ソースコード
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"))