結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 obakyanobakyan
提出日時 2023-04-21 22:27:13
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 1,654 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 23,936 KB
最終ジャッジ日時 2024-04-25 19:01:24
合計ジャッジ時間 7,185 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 170 ms
15,616 KB
testcase_04 AC 174 ms
22,224 KB
testcase_05 AC 83 ms
21,248 KB
testcase_06 AC 114 ms
13,056 KB
testcase_07 AC 106 ms
12,288 KB
testcase_08 AC 59 ms
23,040 KB
testcase_09 AC 109 ms
18,764 KB
testcase_10 AC 69 ms
8,704 KB
testcase_11 AC 38 ms
6,944 KB
testcase_12 AC 46 ms
6,940 KB
testcase_13 AC 137 ms
14,208 KB
testcase_14 AC 59 ms
7,808 KB
testcase_15 AC 58 ms
14,720 KB
testcase_16 AC 97 ms
11,904 KB
testcase_17 AC 46 ms
7,168 KB
testcase_18 AC 171 ms
22,512 KB
testcase_19 AC 159 ms
21,736 KB
testcase_20 AC 123 ms
15,044 KB
testcase_21 AC 84 ms
10,368 KB
testcase_22 AC 107 ms
13,696 KB
testcase_23 AC 99 ms
11,136 KB
testcase_24 AC 89 ms
14,208 KB
testcase_25 AC 124 ms
21,992 KB
testcase_26 AC 41 ms
22,016 KB
testcase_27 AC 59 ms
7,424 KB
testcase_28 AC 58 ms
14,592 KB
testcase_29 AC 91 ms
20,864 KB
testcase_30 AC 55 ms
8,832 KB
testcase_31 AC 86 ms
13,824 KB
testcase_32 AC 84 ms
20,224 KB
testcase_33 AC 173 ms
20,596 KB
testcase_34 AC 119 ms
15,872 KB
testcase_35 AC 24 ms
13,568 KB
testcase_36 AC 81 ms
11,904 KB
testcase_37 AC 115 ms
12,288 KB
testcase_38 AC 24 ms
6,940 KB
testcase_39 AC 34 ms
6,940 KB
testcase_40 AC 25 ms
11,648 KB
testcase_41 AC 149 ms
16,640 KB
testcase_42 AC 104 ms
14,592 KB
testcase_43 AC 141 ms
21,760 KB
testcase_44 AC 204 ms
23,772 KB
testcase_45 AC 209 ms
23,936 KB
testcase_46 AC 200 ms
23,612 KB
testcase_47 AC 200 ms
23,916 KB
testcase_48 AC 216 ms
23,896 KB
testcase_49 AC 195 ms
23,748 KB
testcase_50 AC 183 ms
22,912 KB
testcase_51 AC 166 ms
23,296 KB
testcase_52 AC 192 ms
23,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, q = io.read("*n", "*n")
local asked = {}
local parent = {}
local edge = {}
local flag = {}
for i = 1, n do
  asked[i] = false
  parent[i] = i
  edge[i] = {}
  flag[i] = 0
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 a, b, c = {}, {}, {}
for iq = 1, q do
  local ai, bi, ci = io.read("*n", "*n", "*n")
  a[iq], b[iq], c[iq] = ai, bi, ci
  table.insert(edge[ai], iq)
  table.insert(edge[bi], iq)
  local ar, br = uf_findroot(ai), uf_findroot(bi)
  if ar ~= br then
    parent[bi], parent[br] = ar, ar
  end
end
local gmap = {}
for i = 1, n do
  local r = uf_findroot(i)
  gmap[r] = true
end

local function check(spos)
  asked[spos] = true
  local tasks = {spos}
  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] + b[ei] - src
      local ci = c[ei]
      if asked[dst] then
        if ci == 0 then
          if flag[dst] ~= flag[src] then return false end
        else
          if flag[dst] == flag[src] then return false end
        end
      else
        asked[dst] = true
        if ci == 0 then
          flag[dst] = flag[src]
        else
          flag[dst] = 1 - flag[src]
        end
        table.insert(tasks, dst)
      end
    end
  end
  return true
end

local ans = 1
for key, val in pairs(gmap) do
  if check(key) then
    ans = (ans + ans) % 998244353
  else
    print(0) os.exit()
  end
end
print(ans)
0