結果

問題 No.1420 国勢調査 (Easy)
ユーザー 👑 obakyanobakyan
提出日時 2022-03-29 20:53:48
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 1,286 bytes
コンパイル時間 45 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 23,680 KB
最終ジャッジ日時 2024-04-26 06:26:36
合計ジャッジ時間 6,905 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 108 ms
10,240 KB
testcase_03 AC 110 ms
10,112 KB
testcase_04 AC 107 ms
10,240 KB
testcase_05 AC 110 ms
10,240 KB
testcase_06 AC 107 ms
10,240 KB
testcase_07 AC 97 ms
10,112 KB
testcase_08 AC 92 ms
9,984 KB
testcase_09 AC 103 ms
10,112 KB
testcase_10 AC 100 ms
10,112 KB
testcase_11 AC 104 ms
10,112 KB
testcase_12 AC 31 ms
16,128 KB
testcase_13 AC 51 ms
16,512 KB
testcase_14 AC 32 ms
16,128 KB
testcase_15 AC 46 ms
16,512 KB
testcase_16 AC 45 ms
16,512 KB
testcase_17 AC 50 ms
16,640 KB
testcase_18 AC 44 ms
16,512 KB
testcase_19 AC 42 ms
16,512 KB
testcase_20 AC 44 ms
16,512 KB
testcase_21 AC 46 ms
16,640 KB
testcase_22 AC 209 ms
23,552 KB
testcase_23 AC 215 ms
23,552 KB
testcase_24 AC 223 ms
23,680 KB
testcase_25 AC 212 ms
23,680 KB
testcase_26 AC 219 ms
23,680 KB
testcase_27 AC 141 ms
21,248 KB
testcase_28 AC 140 ms
20,992 KB
testcase_29 AC 149 ms
20,992 KB
testcase_30 AC 144 ms
20,992 KB
testcase_31 AC 135 ms
20,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bxor = bit.bxor

local n, m = io.read("*n", "*n")
local a, b, y = {}, {}, {}
local edge = {}
local parent = {}
for i = 1, n do
  parent[i] = i
  edge[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
for i = 1, m do
  a[i], b[i], y[i] = io.read("*n", "*n", "*n")
  local ra, rb = uf_findroot(a[i]), uf_findroot(b[i])
  parent[rb], parent[b[i]] = ra, ra
  table.insert(edge[a[i]], i)
  table.insert(edge[b[i]], i)
end
local ans = {}
local asked = {}
for i = 1, n do
  asked[i] = false
  ans[i] = 0
end
local tasks = {}
for i = 1, n do
  if uf_findroot(i) == i then
    asked[i] = true
    table.insert(tasks, i)
  end
end
local done = 0
while done < #tasks do
  done = done + 1
  local src = tasks[done]
  for i = 1, #edge[src] do
    local ei = edge[src][i]
    local dst = a[ei] == src and b[ei] or a[ei]
    local yi = y[ei]
    if asked[dst] then
      if bxor(ans[src], ans[dst]) ~= yi then
        print(-1) os.exit()
      end
    else
      asked[dst] = true
      ans[dst] = bxor(ans[src], yi)
      table.insert(tasks, dst)
    end
  end
end
print(table.concat(ans, "\n"))
0