結果
問題 | No.1640 簡単な色塗り |
ユーザー | 👑 obakyan |
提出日時 | 2021-11-23 22:56:22 |
言語 | Lua (LuaJit 2.1.1696795921) |
結果 |
AC
|
実行時間 | 309 ms / 2,000 ms |
コード長 | 2,189 bytes |
コンパイル時間 | 142 ms |
コンパイル使用メモリ | 5,120 KB |
実行使用メモリ | 55,168 KB |
最終ジャッジ日時 | 2024-06-25 20:43:25 |
合計ジャッジ時間 | 18,461 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 195 ms
55,168 KB |
testcase_05 | AC | 190 ms
48,768 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 175 ms
29,696 KB |
testcase_11 | AC | 141 ms
25,600 KB |
testcase_12 | AC | 122 ms
23,808 KB |
testcase_13 | AC | 300 ms
47,104 KB |
testcase_14 | AC | 309 ms
46,336 KB |
testcase_15 | AC | 88 ms
17,152 KB |
testcase_16 | AC | 117 ms
22,016 KB |
testcase_17 | AC | 251 ms
39,936 KB |
testcase_18 | AC | 16 ms
5,760 KB |
testcase_19 | AC | 124 ms
23,680 KB |
testcase_20 | AC | 183 ms
29,440 KB |
testcase_21 | AC | 137 ms
25,216 KB |
testcase_22 | AC | 12 ms
5,376 KB |
testcase_23 | AC | 205 ms
31,232 KB |
testcase_24 | AC | 45 ms
11,520 KB |
testcase_25 | AC | 125 ms
23,680 KB |
testcase_26 | AC | 234 ms
37,760 KB |
testcase_27 | AC | 87 ms
16,512 KB |
testcase_28 | AC | 305 ms
44,800 KB |
testcase_29 | AC | 255 ms
36,864 KB |
testcase_30 | AC | 23 ms
8,064 KB |
testcase_31 | AC | 192 ms
38,528 KB |
testcase_32 | AC | 165 ms
34,944 KB |
testcase_33 | AC | 103 ms
23,168 KB |
testcase_34 | AC | 142 ms
31,232 KB |
testcase_35 | AC | 113 ms
23,040 KB |
testcase_36 | AC | 23 ms
8,192 KB |
testcase_37 | AC | 34 ms
10,496 KB |
testcase_38 | AC | 177 ms
35,584 KB |
testcase_39 | AC | 66 ms
17,280 KB |
testcase_40 | AC | 71 ms
17,664 KB |
testcase_41 | AC | 141 ms
31,488 KB |
testcase_42 | AC | 78 ms
18,944 KB |
testcase_43 | AC | 79 ms
19,712 KB |
testcase_44 | AC | 83 ms
19,200 KB |
testcase_45 | AC | 59 ms
15,872 KB |
testcase_46 | AC | 27 ms
9,472 KB |
testcase_47 | AC | 17 ms
6,784 KB |
testcase_48 | AC | 176 ms
36,224 KB |
testcase_49 | AC | 9 ms
5,376 KB |
testcase_50 | AC | 1 ms
5,376 KB |
testcase_51 | AC | 2 ms
5,376 KB |
testcase_52 | AC | 294 ms
48,768 KB |
testcase_53 | AC | 291 ms
48,896 KB |
07_evil_01.txt | AC | 798 ms
98,432 KB |
07_evil_02.txt | AC | 1,302 ms
153,472 KB |
ソースコード
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 if a ~= b then edgecnt[b] = edgecnt[b] + 1 end 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"))